Skip to content

Instantly share code, notes, and snippets.

@kinncj
Created September 17, 2015 20:07
Show Gist options
  • Select an option

  • Save kinncj/dbf5cd1760164f058ad1 to your computer and use it in GitHub Desktop.

Select an option

Save kinncj/dbf5cd1760164f058ad1 to your computer and use it in GitHub Desktop.
Enum
<?php
abstract class Enum extends \ArrayObject
{
public final function __construct()
{
$constantsToRemove = ['STD_PROP_LIST', 'ARRAY_AS_PROPS'];
$constants = (new \ReflectionObject($this))->getConstants();
foreach ($constants as $constant => $value) {
if (!in_array($constant, $constantsToRemove)) {
$this[$constant] = $value;
}
}
}
}
<?php
$ua = new UserActions;
var_dump($ua->getArrayCopy());
foreach ($ua as $constant => $value) {
echo sprintf('Constant "%s", Value "%s"', $constant, $value).PHP_EOL;
}
<?php
class UserActions extends Enum
{
const UPDATED = 'user.updated';
const CREATED = 'user.created';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment