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"; return self::$enums[$id] ?? self::$enums[$id] = new static($name); } public function __toString() { 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 git.master, git.master_jit, rfc.property-hooks
bool(true) bool(true) bool(false) bool(false) string(3) "RED" string(5) "GREEN" Fatal error: Uncaught TypeError: Undefined enum Color::GEERN() in /in/aoPUl:9 Stack trace: #0 /in/aoPUl(16): Enum->__construct('GEERN') #1 /in/aoPUl(44): Enum::__callStatic('GEERN', Array) #2 {main} thrown in /in/aoPUl on line 9
Process exited with code 255.

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
61.61 ms | 401 KiB | 8 Q