Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * PHP: Fake Enums with validation method.
- * @author MrLore <[email protected]>
- */
- final class FakeEnums
- {
- const ENUM_ONE = 'one';
- const ENUM_TWO = 'two';
- const ENUM_THREE = 'three';
- private static $validConstants;
- private function __construct(){ /* Enum: Instantiation not allowed. */}
- /**
- * Returns true if the specified type is an emum of this class
- *
- * @param mixed $type The value to compare to this class' constants.
- * @return boolean True if $type is a constant of this class.
- */
- public static function isValid($type)
- {
- return in_array($type, (isset(self::$validConstants) ? self::$validConstants : (self::$validConstants = _rv(new ReflectionClass(__CLASS__))->getConstants())));
- }//End isValid()
- }//End FakeEnums
- /**
- * Allows chaining of methods from their declaration.
- *
- * e.g:
- *
- * new ReflectionClass(__CLASS__)->getConstants();
- * Produces: syntax error, unexpected '->' (T_OBJECT_OPERATOR)
- *
- * _rv(new ReflectionClass(__CLASS__))->getConstants();
- * Works fine.
- */
- function _rv($val)
- {
- return $val;
- }//End _rv()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement