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!"); } } 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());
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/qlefI
function name:  (null)
number of ops:  15
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    7     0  E >   DECLARE_CLASS                                            'fema%5Cenums%5Cenum'
   70     1        DECLARE_CLASS                                            'fema%5Cenums%5Cdocenum', 'fema%5Cenums%5Cenum'
  121     2        DECLARE_CLASS                                            'fema%5Cenums%5Cworkdays', 'fema%5Cenums%5Cdocenum'
  129     3        DECLARE_CLASS                                            'fema%5Cenums%5Cdays', 'fema%5Cenums%5Cworkdays'
  133     4        INIT_NS_FCALL_BY_NAME                                    'fema%5Cenums%5Cvar_dump'
          5        INIT_STATIC_METHOD_CALL                                  'fema%5Cenums%5CWorkDays', 'getAll'
          6        DO_FCALL                                      0  $0      
          7        SEND_VAR_NO_REF_EX                                       $0
          8        DO_FCALL                                      0          
  135     9        INIT_NS_FCALL_BY_NAME                                    'fema%5Cenums%5Cvar_dump'
         10        INIT_STATIC_METHOD_CALL                                  'fema%5Cenums%5CDays', 'getAll'
         11        DO_FCALL                                      0  $2      
         12        SEND_VAR_NO_REF_EX                                       $2
         13        DO_FCALL                                      0          
         14      > RETURN                                                   1

Class fema\enums\Enum:
Function name:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/qlefI
function name:  name
number of ops:  5
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   15     0  E >   FETCH_OBJ_R                                      ~0      'name'
          1        VERIFY_RETURN_TYPE                                       ~0
          2      > RETURN                                                   ~0
   16     3*       VERIFY_RETURN_TYPE                                       
          4*     > RETURN                                                   null

End of function name

Function ordinal:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/qlefI
function name:  ordinal
number of ops:  5
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   22     0  E >   FETCH_OBJ_R                                      ~0      'ordinal'
          1        VERIFY_RETURN_TYPE                                       ~0
          2      > RETURN                                                   ~0
   23     3*       VERIFY_RETURN_TYPE                                       
          4*     > RETURN                                                   null

End of function ordinal

Function __tostring:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/qlefI
function name:  __toString
number of ops:  6
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   30     0  E >   INIT_METHOD_CALL                                         'name'
          1        DO_FCALL                                      0  $0      
          2        VERIFY_RETURN_TYPE                                       $0
          3      > RETURN                                                   $0
   31     4*       VERIFY_RETURN_TYPE                                       
          5*     > RETURN                                                   null

End of function __tostring

Function getall:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/qlefI
function name:  getAll
number of ops:  2
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   36     0  E >   VERIFY_RETURN_TYPE                                       
          1      > RETURN                                                   null

End of function getall

Function fromname:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 13
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 13
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 12
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 13
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 13
filename:       /in/qlefI
function name:  fromName
number of ops:  26
compiled vars:  !0 = $name, !1 = $item
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   45     0  E >   RECV                                             !0      
   46     1        INIT_STATIC_METHOD_CALL                                  'getAll'
          2        DO_FCALL                                      0  $2      
          3      > FE_RESET_R                                       $3      $2, ->13
          4    > > FE_FETCH_R                                               $3, !1, ->13
   47     5    >   INIT_METHOD_CALL                                         !1, 'name'
          6        DO_FCALL                                      0  $4      
          7        IS_IDENTICAL                                             !0, $4
          8      > JMPZ                                                     ~5, ->12
   48     9    >   VERIFY_RETURN_TYPE                                       !1
         10        FE_FREE                                                  $3
         11      > RETURN                                                   !1
   46    12    > > JMP                                                      ->4
         13    >   FE_FREE                                                  $3
   51    14        NEW                                              $6      'fema%5Cenums%5CEnumNotFoundException'
         15        FETCH_CLASS_NAME                                 ~7      
         16        CONCAT                                           ~8      'The+enum+', ~7
         17        ROPE_INIT                                     3  ~10     '+with+name+%27'
         18        ROPE_ADD                                      1  ~10     ~10, !0
         19        ROPE_END                                      2  ~9      ~10, '%27+wasn%27t+found%21'
         20        CONCAT                                           ~12     ~8, ~9
         21        SEND_VAL_EX                                              ~12
         22        DO_FCALL                                      0          
         23      > THROW                                         0          $6
   52    24*       VERIFY_RETURN_TYPE                                       
         25*     > RETURN                                                   null

End of function fromname

Function fromordinal:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 13
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 13
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 12
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 13
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 13
filename:       /in/qlefI
function name:  fromOrdinal
number of ops:  26
compiled vars:  !0 = $ordinal, !1 = $item
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   60     0  E >   RECV                                             !0      
   61     1        INIT_STATIC_METHOD_CALL                                  'getAll'
          2        DO_FCALL                                      0  $2      
          3      > FE_RESET_R                                       $3      $2, ->13
          4    > > FE_FETCH_R                                               $3, !1, ->13
   62     5    >   INIT_METHOD_CALL                                         !1, 'ordinal'
          6        DO_FCALL                                      0  $4      
          7        IS_IDENTICAL                                             !0, $4
          8      > JMPZ                                                     ~5, ->12
   63     9    >   VERIFY_RETURN_TYPE                                       !1
         10        FE_FREE                                                  $3
         11      > RETURN                                                   !1
   61    12    > > JMP                                                      ->4
         13    >   FE_FREE                                                  $3
   66    14        NEW                                              $6      'fema%5Cenums%5CEnumNotFoundException'
         15        FETCH_CLASS_NAME                                 ~7      
         16        CONCAT                                           ~8      'The+enum+', ~7
         17        ROPE_INIT                                     3  ~10     '+with+ordinal+%27'
         18        ROPE_ADD                                      1  ~10     ~10, !0
         19        ROPE_END                                      2  ~9      ~10, '%27+wasn%27t+found%21'
         20        CONCAT                                           ~12     ~8, ~9
         21        SEND_VAL_EX                                              ~12
         22        DO_FCALL                                      0          
         23      > THROW                                         0          $6
   67    24*       VERIFY_RETURN_TYPE                                       
         25*     > RETURN                                                   null

End of function fromordinal

End of class fema\enums\Enum.

Class fema\enums\DocEnum:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/qlefI
function name:  __construct
number of ops:  1
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   76     0  E > > RETURN                                                   null

End of function __construct

Function getall:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/qlefI
function name:  getAll
number of ops:  8
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   82     0  E >   INIT_STATIC_METHOD_CALL                                  'getOrLoad'
          1        FETCH_CLASS_NAME                                 ~0      
          2        SEND_VAL_EX                                              ~0
          3        DO_FCALL                                      0  $1      
          4        VERIFY_RETURN_TYPE                                       $1
          5      > RETURN                                                   $1
   83     6*       VERIFY_RETURN_TYPE                                       
          7*     > RETURN                                                   null

End of function getall

Function getorload:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 61
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 24
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 25
Branch analysis from position: 25
2 jumps found. (Code = 43) Position 1 = 34, Position 2 = 58
Branch analysis from position: 34
2 jumps found. (Code = 43) Position 1 = 42, Position 2 = 58
Branch analysis from position: 42
2 jumps found. (Code = 77) Position 1 = 44, Position 2 = 57
Branch analysis from position: 44
2 jumps found. (Code = 78) Position 1 = 45, Position 2 = 57
Branch analysis from position: 45
1 jumps found. (Code = 42) Position 1 = 44
Branch analysis from position: 44
Branch analysis from position: 57
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 57
Branch analysis from position: 58
Branch analysis from position: 58
Branch analysis from position: 24
2 jumps found. (Code = 43) Position 1 = 34, Position 2 = 58
Branch analysis from position: 34
Branch analysis from position: 58
Branch analysis from position: 61
filename:       /in/qlefI
function name:  getOrLoad
number of ops:  65
compiled vars:  !0 = $class, !1 = $cls, !2 = $enums, !3 = $ordinal, !4 = $doc, !5 = $matches, !6 = $name, !7 = $enum
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   85     0  E >   RECV                                             !0      
   86     1        FETCH_STATIC_PROP_IS                             ~8      'enums'
          2        ISSET_ISEMPTY_DIM_OBJ                         0  ~9      ~8, !0
          3        BOOL_NOT                                         ~10     ~9
          4      > JMPZ                                                     ~10, ->61
   87     5    >   NEW                                              $11     'ReflectionClass'
          6        SEND_VAR_EX                                              !0
          7        DO_FCALL                                      0          
          8        ASSIGN                                                   !1, $11
   88     9        INIT_METHOD_CALL                                         !1, 'getParentClass'
         10        DO_FCALL                                      0  $14     
         11        INIT_METHOD_CALL                                         $14, 'getName'
         12        DO_FCALL                                      0  $15     
         13        IS_NOT_IDENTICAL                                         $15, 'fema%5Cenums%5CDocEnum'
         14      > JMPZ                                                     ~16, ->24
   89    15    >   INIT_STATIC_METHOD_CALL                                  'getOrLoad'
         16        INIT_METHOD_CALL                                         !1, 'getParentClass'
         17        DO_FCALL                                      0  $17     
         18        INIT_METHOD_CALL                                         $17, 'getName'
         19        DO_FCALL                                      0  $18     
         20        SEND_VAR_NO_REF_EX                                       $18
         21        DO_FCALL                                      0  $19     
         22        ASSIGN                                                   !2, $19
         23      > JMP                                                      ->25
   91    24    >   ASSIGN                                                   !2, <array>
   93    25    >   INIT_NS_FCALL_BY_NAME                                    'fema%5Cenums%5Ccount'
         26        SEND_VAR_EX                                              !2
         27        DO_FCALL                                      0  $22     
         28        ASSIGN                                                   !3, $22
   95    29        INIT_METHOD_CALL                                         !1, 'getDocComment'
         30        DO_FCALL                                      0  $24     
         31        ASSIGN                                                   !4, $24
   96    32        TYPE_CHECK                                  1018          !4
         33      > JMPZ                                                     ~26, ->58
   97    34    >   ASSIGN                                                   !5, <array>
   98    35        INIT_NS_FCALL_BY_NAME                                    'fema%5Cenums%5Cpreg_match_all'
         36        SEND_VAL_EX                                              '%2F%5E%5Cs%2A%5C%2A%5Cs%2A%40method%5Cs%2Bstatic%5Cs%2B%28%5BA-Z%5D%2B%29%5C%28%5Cs%2A%5C%29%5Cs%2A%24%2Fm'
         37        SEND_VAR_EX                                              !4
         38        SEND_VAR_EX                                              !5
         39        DO_FCALL                                      0  $28     
         40        IS_SMALLER                                               0, $28
         41      > JMPZ                                                     ~29, ->58
   99    42    >   FETCH_DIM_R                                      ~30     !5, 1
         43      > FE_RESET_R                                       $31     ~30, ->57
         44    > > FE_FETCH_R                                               $31, !6, ->57
  100    45    >   FETCH_CLASS                                   0  $32     !0
         46        NEW                                              $33     $32
         47        DO_FCALL                                      0          
         48        ASSIGN                                                   !7, $33
  101    49        ASSIGN_OBJ                                               !7, 'name'
         50        OP_DATA                                                  !6
  102    51        POST_INC                                         ~38     !3
         52        ASSIGN_OBJ                                               !7, 'ordinal'
         53        OP_DATA                                                  ~38
  103    54        ASSIGN_DIM                                               !2
         55        OP_DATA                                                  !7
   99    56      > JMP                                                      ->44
         57    >   FE_FREE                                                  $31
  107    58    >   FETCH_STATIC_PROP_W          unknown             $40     'enums'
         59        ASSIGN_DIM                                               $40, !0
         60        OP_DATA                                                  !2
  109    61    >   FETCH_STATIC_PROP_R          unknown             ~42     'enums'
         62        FETCH_DIM_R                                      ~43     ~42, !0
         63      > RETURN                                                   ~43
  110    64*     > RETURN                                                   null

End of function getorload

End of class fema\enums\DocEnum.

Class fema\enums\WorkDays: [no user functions]
Class fema\enums\Days: [no user functions]

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
166.77 ms | 1412 KiB | 19 Q