3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace Derp; abstract class Enum { protected $value; protected static $possibleValues; protected static function getPossibleValues() { if (static::$possibleValues) { return static::$possibleValues; } $reflectionClass = new \ReflectionClass(get_called_class()); static::$possibleValues = $reflectionClass->getConstants(); return static::$possibleValues; } /** * @param $name * @param $arguments * @return static * @throws \Exception */ public static function __callStatic($name, $arguments) { $possibleValues = static::getPossibleValues(); if (!array_key_exists($name, $possibleValues)) { throw new \Exception; } if (count($arguments) > 0) { throw new \Exception; } return self::createInstance($name, $possibleValues[$name]); } /** * @param $value * @return static */ protected static function createInstance($value) { $enum = new static; $enum->value = $value; return $enum; } protected function getValue() { return $this->value; } public function equals(Enum $otherValue) { return get_class($otherValue) === get_called_class() && $otherValue->getValue() === $this->getValue(); } public static function all() { return array_map( function($value) { return static::createInstance($value); }, static::getPossibleValues() ); } } class Day extends Enum { const MONDAY = 'monday'; const FRIDAY = 'friday'; } $monday = Day::MONDAY(); var_dump($monday->equals(Day::FRIDAY())); var_dump(Day::all());
Output for 5.4.30 - 5.4.45, 5.5.14 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
bool(false) array(2) { ["MONDAY"]=> object(Derp\Day)#3 (1) { ["value":protected]=> string(6) "monday" } ["FRIDAY"]=> object(Derp\Day)#4 (1) { ["value":protected]=> string(6) "friday" } }
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 bool(false) array(2) { ["MONDAY"]=> object(Derp\Day)#3 (1) { ["value":protected]=> string(6) "monday" } ["FRIDAY"]=> object(Derp\Day)#4 (1) { ["value":protected]=> string(6) "friday" } }
Output for 5.4.0 - 5.4.29, 5.5.0 - 5.5.13
bool(false) Fatal error: Cannot instantiate abstract class Derp\Enum in /in/YZ6En on line 49
Process exited with code 255.
Output for 5.3.0 - 5.3.29
bool(false) Fatal error: Cannot access static:: when no class scope is active in /in/YZ6En on line 68
Process exited with code 255.
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17
Parse error: syntax error, unexpected T_STRING in /in/YZ6En on line 3
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, 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_STRING in /in/YZ6En on line 3
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/YZ6En on line 3
Process exited with code 255.

preferences:
259.44 ms | 401 KiB | 459 Q