3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * @method static LocationType GROUP_OF_STATES() * @method static LocationType STATE() * @method static LocationType REGION() * @method static LocationType CITY() * @method static LocationType DISTRICT() * @method static LocationType ADDRESS() */ class LocationType extends EnumBASE { const GROUP_OF_STATES = "GROUP-OF-STATES"; const STATE = "STATE"; const REGION = "REGION"; const CITY = "CITY"; const DISTRICT = "DISTRICT"; const ADDRESS = "ADDRESS"; } var_dump(LocationType::GROUP_OF_STATES()); var_dump(LocationType::REGION()); var_dump(LocationType::ADDRESS()); abstract class EnumBASE { static function getAll() { $x = new ReflectionClass(static::class); foreach($x->getConstants() as $constantValue) yield new static($constantValue); } private $name; private $value; /** * Constructs by value * * @param int|float|string|bool|null $enumValue */ final function __construct($enumValue) { $valid = false; foreach((new ReflectionClass($this))->getConstants() as $constantName => $constantValue) { if($constantValue === $enumValue) { $this->name = $constantName; $this->value = $constantValue; $valid = true; break; } } if(!$valid) throw new ExceptionDomain("Enum is invalid."); } /** * Constructs by name * * @param string $method * @param mixed[] $params * @return static */ final static function __callStatic($method, $params) { return new static(constant(get_called_class() . "::" . $method)); } /** * TODO * * @return string */ function getName() { return $this->name; } /** * TODO * * @return mixed */ function getValue() { return $this->value; } function __toString() { return $this->getName(); } }

preferences:
52.38 ms | 402 KiB | 5 Q