3v4l.org

run code in 300+ PHP versions simultaneously
<?php abstract class Enum { private $name; private static $enums; private function __construct($name) { $this->name = $name; } /** * Returns an assoc. array of ['ENUM_NAME' => $ENUM_VALUE] for all enum values. * @return array */ public static function getAll(): array { $class = static::class; if (!isset(self::$enums[$class])) static::init(); return self::$enums[$class]; } /** * Return an enum value (object) from a string name. * @return $this */ public static function fromString($name) { return static::__callStatic($name, []); } public function __toString() { return $this->name; } public static function __callStatic($name, $args) { $class = static::class; if (!isset(self::$enums[$class])) static::init(); if (!isset(self::$enums[$class][$name])) { throw new \TypeError('Undefined enum ' . $class . '::' . $name . '()'); } return self::$enums[$class][$name]; } private static function init() { $class = static::class; if ($class === __CLASS__) { throw new \Exception('Do not invoke methods directly on class Enum.'); } $doc = (new \ReflectionClass($class))->getDocComment(); if (preg_match_all('/@method\s+static\s+(\w+)/i', $doc, $matches)) { foreach ($matches[1] as $name) { self::$enums[$class][$name] = new static($name); } } else { throw new \Exception('Please provide a PHPDoc for ' . $class . ' with a static @method for each enum value.'); } } } /** * @method static RED() * @method static GREEN() * @method static BLUE() */ class Color extends Enum {} $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); var_dump($red === Color::fromString('RED')); var_dump(Color::getAll()); try { $geern = Color::GEERN(); // Intentional typo. Throws. } catch (\Throwable $e) { echo get_class($e) . ': ' . $e->getMessage(). "\n"; } try { Enum::getAll(); // Shouldn't call methods directly on Enum. Throws. } catch (\Throwable $e) { echo get_class($e) . ': ' . $e->getMessage(). "\n"; }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 59
Branch analysis from position: 59
1 jumps found. (Code = 42) Position 1 = 70
Branch analysis from position: 70
1 jumps found. (Code = 62) Position 1 = -2
Found catch point at position: 51
Branch analysis from position: 51
2 jumps found. (Code = 107) Position 1 = 52, Position 2 = -2
Branch analysis from position: 52
1 jumps found. (Code = 42) Position 1 = 70
Branch analysis from position: 70
Found catch point at position: 62
Branch analysis from position: 62
2 jumps found. (Code = 107) Position 1 = 63, Position 2 = -2
Branch analysis from position: 63
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ZQhMn
function name:  (null)
number of ops:  71
compiled vars:  !0 = $red, !1 = $red2, !2 = $green, !3 = $geern, !4 = $e
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   DECLARE_CLASS                                            'enum'
   65     1        DECLARE_CLASS                                            'color', 'enum'
   67     2        INIT_STATIC_METHOD_CALL                                  'Color', 'RED'
          3        DO_FCALL                                      0  $5      
          4        ASSIGN                                                   !0, $5
   68     5        INIT_STATIC_METHOD_CALL                                  'Color', 'RED'
          6        DO_FCALL                                      0  $7      
          7        ASSIGN                                                   !1, $7
   69     8        INIT_STATIC_METHOD_CALL                                  'Color', 'GREEN'
          9        DO_FCALL                                      0  $9      
         10        ASSIGN                                                   !2, $9
   71    11        INIT_FCALL                                               'var_dump'
         12        IS_IDENTICAL                                     ~11     !0, !1
         13        SEND_VAL                                                 ~11
         14        DO_ICALL                                                 
   72    15        INIT_FCALL                                               'var_dump'
         16        IS_EQUAL                                         ~13     !0, !1
         17        SEND_VAL                                                 ~13
         18        DO_ICALL                                                 
   73    19        INIT_FCALL                                               'var_dump'
         20        IS_IDENTICAL                                     ~15     !0, !2
         21        SEND_VAL                                                 ~15
         22        DO_ICALL                                                 
   74    23        INIT_FCALL                                               'var_dump'
         24        IS_EQUAL                                         ~17     !0, !2
         25        SEND_VAL                                                 ~17
         26        DO_ICALL                                                 
   75    27        INIT_FCALL                                               'var_dump'
         28        CAST                                          6  ~19     !0
         29        SEND_VAL                                                 ~19
         30        DO_ICALL                                                 
   76    31        INIT_FCALL                                               'var_dump'
         32        CAST                                          6  ~21     !2
         33        SEND_VAL                                                 ~21
         34        DO_ICALL                                                 
   77    35        INIT_FCALL                                               'var_dump'
         36        INIT_STATIC_METHOD_CALL                                  'Color', 'fromString'
         37        SEND_VAL_EX                                              'RED'
         38        DO_FCALL                                      0  $23     
         39        IS_IDENTICAL                                     ~24     !0, $23
         40        SEND_VAL                                                 ~24
         41        DO_ICALL                                                 
   78    42        INIT_FCALL                                               'var_dump'
         43        INIT_STATIC_METHOD_CALL                                  'Color', 'getAll'
         44        DO_FCALL                                      0  $26     
         45        SEND_VAR                                                 $26
         46        DO_ICALL                                                 
   82    47        INIT_STATIC_METHOD_CALL                                  'Color', 'GEERN'
         48        DO_FCALL                                      0  $28     
         49        ASSIGN                                                   !3, $28
         50      > JMP                                                      ->59
   83    51  E > > CATCH                                       last         'Throwable'
   84    52    >   GET_CLASS                                        ~30     !4
         53        CONCAT                                           ~31     ~30, '%3A+'
         54        INIT_METHOD_CALL                                         !4, 'getMessage'
         55        DO_FCALL                                      0  $32     
         56        CONCAT                                           ~33     ~31, $32
         57        CONCAT                                           ~34     ~33, '%0A'
         58        ECHO                                                     ~34
   88    59    >   INIT_STATIC_METHOD_CALL                                  'Enum', 'getAll'
         60        DO_FCALL                                      0          
         61      > JMP                                                      ->70
   89    62  E > > CATCH                                       last         'Throwable'
   90    63    >   GET_CLASS                                        ~36     !4
         64        CONCAT                                           ~37     ~36, '%3A+'
         65        INIT_METHOD_CALL                                         !4, 'getMessage'
         66        DO_FCALL                                      0  $38     
         67        CONCAT                                           ~39     ~37, $38
         68        CONCAT                                           ~40     ~39, '%0A'
         69        ECHO                                                     ~40
   91    70    > > RETURN                                                   1

Class Enum:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ZQhMn
function name:  __construct
number of ops:  4
compiled vars:  !0 = $name
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    6     0  E >   RECV                                             !0      
    7     1        ASSIGN_OBJ                                               'name'
          2        OP_DATA                                                  !0
    8     3      > RETURN                                                   null

End of function __construct

Function getall:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 8
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
filename:       /in/ZQhMn
function name:  getAll
number of ops:  14
compiled vars:  !0 = $class
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   15     0  E >   FETCH_CLASS_NAME                                 ~1      
          1        ASSIGN                                                   !0, ~1
   16     2        FETCH_STATIC_PROP_IS                             ~3      'enums'
          3        ISSET_ISEMPTY_DIM_OBJ                         0  ~4      ~3, !0
          4        BOOL_NOT                                         ~5      ~4
          5      > JMPZ                                                     ~5, ->8
          6    >   INIT_STATIC_METHOD_CALL                                  'init'
          7        DO_FCALL                                      0          
   17     8    >   FETCH_STATIC_PROP_R          unknown             ~7      'enums'
          9        FETCH_DIM_R                                      ~8      ~7, !0
         10        VERIFY_RETURN_TYPE                                       ~8
         11      > RETURN                                                   ~8
   18    12*       VERIFY_RETURN_TYPE                                       
         13*     > RETURN                                                   null

End of function getall

Function fromstring:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ZQhMn
function name:  fromString
number of ops:  7
compiled vars:  !0 = $name
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   24     0  E >   RECV                                             !0      
   25     1        INIT_STATIC_METHOD_CALL                                  '__callStatic'
          2        SEND_VAR_EX                                              !0
          3        SEND_VAL_EX                                              <array>
          4        DO_FCALL                                      0  $1      
          5      > RETURN                                                   $1
   26     6*     > RETURN                                                   null

End of function fromstring

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

End of function __tostring

Function __callstatic:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 10
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 23
Branch analysis from position: 15
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 23
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
filename:       /in/ZQhMn
function name:  __callStatic
number of ops:  28
compiled vars:  !0 = $name, !1 = $args, !2 = $class
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   32     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   33     2        FETCH_CLASS_NAME                                 ~3      
          3        ASSIGN                                                   !2, ~3
   34     4        FETCH_STATIC_PROP_IS                             ~5      'enums'
          5        ISSET_ISEMPTY_DIM_OBJ                         0  ~6      ~5, !2
          6        BOOL_NOT                                         ~7      ~6
          7      > JMPZ                                                     ~7, ->10
          8    >   INIT_STATIC_METHOD_CALL                                  'init'
          9        DO_FCALL                                      0          
   35    10    >   FETCH_STATIC_PROP_IS                             ~9      'enums'
         11        FETCH_DIM_IS                                     ~10     ~9, !2
         12        ISSET_ISEMPTY_DIM_OBJ                         0  ~11     ~10, !0
         13        BOOL_NOT                                         ~12     ~11
         14      > JMPZ                                                     ~12, ->23
   36    15    >   NEW                                              $13     'TypeError'
         16        CONCAT                                           ~14     'Undefined+enum+', !2
         17        CONCAT                                           ~15     ~14, '%3A%3A'
         18        CONCAT                                           ~16     ~15, !0
         19        CONCAT                                           ~17     ~16, '%28%29'
         20        SEND_VAL_EX                                              ~17
         21        DO_FCALL                                      0          
         22      > THROW                                         0          $13
   38    23    >   FETCH_STATIC_PROP_R          unknown             ~19     'enums'
         24        FETCH_DIM_R                                      ~20     ~19, !2
         25        FETCH_DIM_R                                      ~21     ~20, !0
         26      > RETURN                                                   ~21
   39    27*     > RETURN                                                   null

End of function __callstatic

Function init:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 8
Branch analysis from position: 4
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 33
Branch analysis from position: 20
2 jumps found. (Code = 77) Position 1 = 22, Position 2 = 31
Branch analysis from position: 22
2 jumps found. (Code = 78) Position 1 = 23, Position 2 = 31
Branch analysis from position: 23
1 jumps found. (Code = 42) Position 1 = 22
Branch analysis from position: 22
Branch analysis from position: 31
1 jumps found. (Code = 42) Position 1 = 39
Branch analysis from position: 39
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 31
Branch analysis from position: 33
1 jumps found. (Code = 108) Position 1 = -2
filename:       /in/ZQhMn
function name:  init
number of ops:  40
compiled vars:  !0 = $class, !1 = $doc, !2 = $matches, !3 = $name
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   42     0  E >   FETCH_CLASS_NAME                                 ~4      
          1        ASSIGN                                                   !0, ~4
   44     2        IS_IDENTICAL                                             !0, 'Enum'
          3      > JMPZ                                                     ~6, ->8
   45     4    >   NEW                                              $7      'Exception'
          5        SEND_VAL_EX                                              'Do+not+invoke+methods+directly+on+class+Enum.'
          6        DO_FCALL                                      0          
          7      > THROW                                         0          $7
   48     8    >   NEW                                              $9      'ReflectionClass'
          9        SEND_VAR_EX                                              !0
         10        DO_FCALL                                      0          
         11        INIT_METHOD_CALL                                         $9, 'getDocComment'
         12        DO_FCALL                                      0  $11     
         13        ASSIGN                                                   !1, $11
   50    14        INIT_FCALL                                               'preg_match_all'
         15        SEND_VAL                                                 '%2F%40method%5Cs%2Bstatic%5Cs%2B%28%5Cw%2B%29%2Fi'
         16        SEND_VAR                                                 !1
         17        SEND_REF                                                 !2
         18        DO_ICALL                                         $13     
         19      > JMPZ                                                     $13, ->33
   51    20    >   FETCH_DIM_R                                      ~14     !2, 1
         21      > FE_RESET_R                                       $15     ~14, ->31
         22    > > FE_FETCH_R                                               $15, !3, ->31
   52    23    >   NEW                          static              $19     
         24        SEND_VAR_EX                                              !3
         25        DO_FCALL                                      0          
         26        FETCH_STATIC_PROP_W          unknown             $16     'enums'
         27        FETCH_DIM_W                                      $17     $16, !0
         28        ASSIGN_DIM                                               $17, !3
         29        OP_DATA                                                  $19
   51    30      > JMP                                                      ->22
         31    >   FE_FREE                                                  $15
         32      > JMP                                                      ->39
   55    33    >   NEW                                              $21     'Exception'
         34        CONCAT                                           ~22     'Please+provide+a+PHPDoc+for+', !0
         35        CONCAT                                           ~23     ~22, '+with+a+static+%40method+for+each+enum+value.'
         36        SEND_VAL_EX                                              ~23
         37        DO_FCALL                                      0          
         38      > THROW                                         0          $21
   57    39    > > RETURN                                                   null

End of function init

End of class Enum.

Class Color: [no user functions]

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
171.9 ms | 1412 KiB | 17 Q