3v4l.org

run code in 300+ PHP versions simultaneously
<?php abstract class Enum { private $name; protected $allow = []; private static $enums; private function __construct($name) { if (!in_array($name, $this->allow, true)) { throw new \TypeError('Undefined enum ' . get_class($this) . '::' . $name . '()'); } $this->name = $name; } public static function __callStatic($name, $args) { $id = static::class . ",$name"; if (!isset(self::$enums[$id])) { $enum = new static($name); self::$enums[$id] = $name; } return self::$enums[$id]; } public function __toString() { echo 'I am invoked'; return $this->name; } } /** * @method static RED() * @method static GREEN() * @method static BLUE() */ class Color extends Enum { protected $allow = ['RED', 'GREEN', 'BLUE']; } $red = Color::RED(); $red2 = Color::RED(); $green = Color::GREEN(); var_dump($red === $red2); var_dump($red == $red2); var_dump($red === $green); var_dump($red == $green); var_dump((string) $red); var_dump((string) $green); $geern = Color::GEERN(); // Intentional typo. Throws.
Output for 7.0.0 - 7.0.20, 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.27, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
bool(true) bool(true) bool(false) bool(false) string(3) "RED" string(5) "GREEN" Fatal error: Uncaught TypeError: Undefined enum Color::GEERN() in /in/bc8OW:9 Stack trace: #0 /in/bc8OW(18): Enum->__construct('GEERN') #1 /in/bc8OW(51): Enum::__callStatic('GEERN', Array) #2 {main} thrown in /in/bc8OW on line 9
Process exited with code 255.
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(true) bool(true) bool(false) bool(false) string(3) "RED" string(5) "GREEN" Fatal error: Uncaught TypeError: Undefined enum Color::GEERN() in /in/bc8OW:9 Stack trace: #0 /in/bc8OW(18): Enum->__construct('GEERN') #1 /in/bc8OW(51): Enum::__callStatic('GEERN', Array) #2 {main} thrown in /in/bc8OW on line 9
Process exited with code 255.

preferences:
220.28 ms | 401 KiB | 217 Q