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(); } }
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
Fatal error: Uncaught Error: Class "EnumBASE" not found in /in/YG0ml:11 Stack trace: #0 {main} thrown in /in/YG0ml on line 11
Process exited with code 255.
Output for 5.5.0 - 5.5.38, 5.6.0 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.10, 7.2.0 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33
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.1.6, 5.2.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.3.11, 4.4.0 - 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.

preferences:
252.48 ms | 401 KiB | 349 Q