3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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(); } }
Output for 5.5.0 - 7.2.0
object(LocationType)#1 (2) { ["name":"EnumBASE":private]=> string(15) "GROUP_OF_STATES" ["value":"EnumBASE":private]=> string(15) "GROUP-OF-STATES" } object(LocationType)#1 (2) { ["name":"EnumBASE":private]=> string(6) "REGION" ["value":"EnumBASE":private]=> string(6) "REGION" } object(LocationType)#1 (2) { ["name":"EnumBASE":private]=> string(7) "ADDRESS" ["value":"EnumBASE":private]=> string(7) "ADDRESS" }
Output for 5.4.0 - 5.4.45
Parse error: syntax error, unexpected 'class' (T_CLASS), expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' in /in/YG0ml on line 32
Process exited with code 255.
Output for 5.3.0 - 5.3.29
Parse error: syntax error, unexpected T_CLASS, expecting T_STRING or T_VARIABLE or '$' in /in/YG0ml on line 32
Process exited with code 255.
Output for 5.1.0 - 5.2.17
Parse error: syntax error, unexpected T_STATIC, expecting ')' in /in/YG0ml on line 32
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_STATIC, expecting ')' in /in/YG0ml on line 32
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_CONST, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/YG0ml on line 13
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_CONST, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/YG0ml on line 13
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/YG0ml on line 13
Process exited with code 255.