3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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!"); } } abstract class DocEnum extends Enum { protected static $enums = []; private function __construct() { } /** * @inheritDoc */ public static function getAll() : array { return static::getOrLoad(static::class); } private static function getOrLoad(string $class) { if (!isset(self::$enums[$class])) { $cls = new \ReflectionClass($class); if ($cls->getParentClass()->getName() !== self::class) { $enums = static::getOrLoad($cls->getParentClass()->getName()); } else { $enums = []; } $ordinal = count($enums); $doc = $cls->getDocComment(); if ($doc !== false) { $matches = []; if (preg_match_all('/^\\s*\\*\\s*@method\\s+static\\s+([A-Z]+)\\(\\s*\\)\\s*$/m', $doc, $matches) > 0) { foreach ($matches[1] as $name) { $enum = new $class(); $enum->name = $name; $enum->ordinal = $ordinal++; $enums[] = $enum; } } } static::$enums[$class] = $enums; } return static::$enums[$class]; } } /** * * @method static MONDAY() * @method static TUESDAY() * @method static WENSDAY() * @method static THURSDAY() * @method static FRIDAY() */ class WorkDays extends DocEnum { } /** * @method static SATURDAY() * @method static SUNDAY() */ class Days extends WorkDays { } var_dump(WorkDays::getAll()); var_dump(Days::getAll());
based on grPiq
Output for 7.0.0 - 7.2.6
array(5) { [0]=> object(fema\enums\WorkDays)#2 (2) { ["name":protected]=> string(6) "MONDAY" ["ordinal":protected]=> int(0) } [1]=> object(fema\enums\WorkDays)#3 (2) { ["name":protected]=> string(7) "TUESDAY" ["ordinal":protected]=> int(1) } [2]=> object(fema\enums\WorkDays)#4 (2) { ["name":protected]=> string(7) "WENSDAY" ["ordinal":protected]=> int(2) } [3]=> object(fema\enums\WorkDays)#5 (2) { ["name":protected]=> string(8) "THURSDAY" ["ordinal":protected]=> int(3) } [4]=> object(fema\enums\WorkDays)#6 (2) { ["name":protected]=> string(6) "FRIDAY" ["ordinal":protected]=> int(4) } } array(7) { [0]=> object(fema\enums\WorkDays)#2 (2) { ["name":protected]=> string(6) "MONDAY" ["ordinal":protected]=> int(0) } [1]=> object(fema\enums\WorkDays)#3 (2) { ["name":protected]=> string(7) "TUESDAY" ["ordinal":protected]=> int(1) } [2]=> object(fema\enums\WorkDays)#4 (2) { ["name":protected]=> string(7) "WENSDAY" ["ordinal":protected]=> int(2) } [3]=> object(fema\enums\WorkDays)#5 (2) { ["name":protected]=> string(8) "THURSDAY" ["ordinal":protected]=> int(3) } [4]=> object(fema\enums\WorkDays)#6 (2) { ["name":protected]=> string(6) "FRIDAY" ["ordinal":protected]=> int(4) } [5]=> object(fema\enums\Days)#7 (2) { ["name":protected]=> string(8) "SATURDAY" ["ordinal":protected]=> int(5) } [6]=> object(fema\enums\Days)#8 (2) { ["name":protected]=> string(6) "SUNDAY" ["ordinal":protected]=> int(6) } }
Output for hhvm-3.15.4
array(5) { [0]=> object(fema\enums\WorkDays)#2 (2) { ["name":protected]=> string(6) "MONDAY" ["ordinal":protected]=> int(0) } [1]=> object(fema\enums\WorkDays)#3 (2) { ["name":protected]=> string(7) "TUESDAY" ["ordinal":protected]=> int(1) } [2]=> object(fema\enums\WorkDays)#4 (2) { ["name":protected]=> string(7) "WENSDAY" ["ordinal":protected]=> int(2) } [3]=> object(fema\enums\WorkDays)#5 (2) { ["name":protected]=> string(8) "THURSDAY" ["ordinal":protected]=> int(3) } [4]=> object(fema\enums\WorkDays)#6 (2) { ["name":protected]=> string(6) "FRIDAY" ["ordinal":protected]=> int(4) } } array(7) { [0]=> object(fema\enums\WorkDays)#2 (2) { ["name":protected]=> string(6) "MONDAY" ["ordinal":protected]=> int(0) } [1]=> object(fema\enums\WorkDays)#3 (2) { ["name":protected]=> string(7) "TUESDAY" ["ordinal":protected]=> int(1) } [2]=> object(fema\enums\WorkDays)#4 (2) { ["name":protected]=> string(7) "WENSDAY" ["ordinal":protected]=> int(2) } [3]=> object(fema\enums\WorkDays)#5 (2) { ["name":protected]=> string(8) "THURSDAY" ["ordinal":protected]=> int(3) } [4]=> object(fema\enums\WorkDays)#6 (2) { ["name":protected]=> string(6) "FRIDAY" ["ordinal":protected]=> int(4) } [5]=> object(fema\enums\Days)#8 (2) { ["name":protected]=> string(8) "SATURDAY" ["ordinal":protected]=> int(5) } [6]=> object(fema\enums\Days)#9 (2) { ["name":protected]=> string(6) "SUNDAY" ["ordinal":protected]=> int(6) } }
Output for 5.6.0 - 5.6.30
Warning: Unsupported declare 'strict_types' in /in/qlefI on line 2 Parse error: syntax error, unexpected ':', expecting ';' or '{' in /in/qlefI on line 14
Process exited with code 255.