Created
September 17, 2015 20:07
-
-
Save kinncj/dbf5cd1760164f058ad1 to your computer and use it in GitHub Desktop.
Enum
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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; | |
| } | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| $ua = new UserActions; | |
| var_dump($ua->getArrayCopy()); | |
| foreach ($ua as $constant => $value) { | |
| echo sprintf('Constant "%s", Value "%s"', $constant, $value).PHP_EOL; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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