3v4l.org

run code in 300+ PHP versions simultaneously
<?php declare(strict_types=1); namespace fema\enums; abstract class Enum { protected $name, $ordinal; /** * @return string the name of the enum */ public function name() : string { return $this->name; } /** * @return int a number representing this enum. Starts at 0. */ public function ordinal() : int { return $this->ordinal; } /** * Alias of name() * @return string the name of the enum */ public function __toString() : string { return $this->name(); } /** * @return Enum[] all the enums that are defined in the class */ public abstract static function getAll() : array; /** * Return a specific enum given its name * @param string $name the name of the enum * @return Enum the enum that corresponds to the passed name * @throws EnumNotFoundException if an enum with the passed name is not found */ public static function fromName(string $name) : Enum { foreach (static::getAll() as $item) { if ($item->name() === $name) { return $item; } } throw new EnumNotFoundException("The enum " . static::class . " with name '$name' wasn't found!"); } /** * Return a specific enum given its ordinal * @param int $ordinal the ordinal of the enum * @return Enum the enum that corresponds to the passed ordinal * @throws EnumNotFoundException if an enum with the passed ordinal is not found */ public static function fromOrdinal(int $ordinal) : Enum { foreach (static::getAll() as $item) { if ($item->ordinal() === $ordinal) { return $item; } } throw new EnumNotFoundException("The enum " . static::class . " with ordinal '$ordinal' wasn't found!"); } } class DocEnum extends Enum { static $enums = null; private function __construct() { } /** * @inheritDoc */ public static function getAll() : array { if (static::$enums === null) { $last = new \ReflectionClass(static::class); /** @var \ReflectionClass[] $classes */ $classes = [$last]; while ($last->getParentClass()->getName() !== self::class) { //Put on top of array array_unshift($classes, $last = new \ReflectionClass($last->getParentClass()->getName())); } $enums = []; $ordinal = 0; foreach ($classes as $class) { $doc = $class->getDocComment(); $matches = []; if (preg_match_all('/^\\s*\\*\\s*@method\\s+static\\s+([A-Z]+)\\(\\)\\s*$/m', $doc, $matches) > 0) { foreach ($matches[1] as $name) { $enum = new DocEnum(); $enum->name = $name; $enum->ordinal = $ordinal++; $enums[] = $enum; } } } static::$enums = $enums; } return static::$enums; } } /** * * @method static MONDAY() * @method static TUESDAY() * @method static WENSDAY() * @method static THURSDAY() * @method static FRIDAY() * @method static SATURDAY() * @method static SUNDAY() */ class MyEnum extends DocEnum { } var_dump(MyEnum::getAll());
Output for 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.27, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
array(7) { [0]=> object(fema\enums\DocEnum)#2 (2) { ["name":protected]=> string(6) "MONDAY" ["ordinal":protected]=> int(0) } [1]=> object(fema\enums\DocEnum)#3 (2) { ["name":protected]=> string(7) "TUESDAY" ["ordinal":protected]=> int(1) } [2]=> object(fema\enums\DocEnum)#4 (2) { ["name":protected]=> string(7) "WENSDAY" ["ordinal":protected]=> int(2) } [3]=> object(fema\enums\DocEnum)#5 (2) { ["name":protected]=> string(8) "THURSDAY" ["ordinal":protected]=> int(3) } [4]=> object(fema\enums\DocEnum)#6 (2) { ["name":protected]=> string(6) "FRIDAY" ["ordinal":protected]=> int(4) } [5]=> object(fema\enums\DocEnum)#7 (2) { ["name":protected]=> string(8) "SATURDAY" ["ordinal":protected]=> int(5) } [6]=> object(fema\enums\DocEnum)#8 (2) { ["name":protected]=> string(6) "SUNDAY" ["ordinal":protected]=> int(6) } }
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 array(7) { [0]=> object(fema\enums\DocEnum)#2 (2) { ["name":protected]=> string(6) "MONDAY" ["ordinal":protected]=> int(0) } [1]=> object(fema\enums\DocEnum)#3 (2) { ["name":protected]=> string(7) "TUESDAY" ["ordinal":protected]=> int(1) } [2]=> object(fema\enums\DocEnum)#4 (2) { ["name":protected]=> string(7) "WENSDAY" ["ordinal":protected]=> int(2) } [3]=> object(fema\enums\DocEnum)#5 (2) { ["name":protected]=> string(8) "THURSDAY" ["ordinal":protected]=> int(3) } [4]=> object(fema\enums\DocEnum)#6 (2) { ["name":protected]=> string(6) "FRIDAY" ["ordinal":protected]=> int(4) } [5]=> object(fema\enums\DocEnum)#7 (2) { ["name":protected]=> string(8) "SATURDAY" ["ordinal":protected]=> int(5) } [6]=> object(fema\enums\DocEnum)#8 (2) { ["name":protected]=> string(6) "SUNDAY" ["ordinal":protected]=> int(6) } }
Output for 5.6.0 - 5.6.40
Warning: Unsupported declare 'strict_types' in /in/shpOC on line 2 Parse error: syntax error, unexpected ':', expecting ';' or '{' in /in/shpOC on line 14
Process exited with code 255.

preferences:
233.37 ms | 401 KiB | 290 Q