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"; if (!isset(self::$enums[$id])) { $enum = new static($name); self::$enums[$id] = $name; } return self::$enums[$id]; } public function __toString() { echo 'I am invoked'; 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.
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/bc8OW
function name:  (null)
number of ops:  39
compiled vars:  !0 = $red, !1 = $red2, !2 = $green, !3 = $geern
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   DECLARE_CLASS                                            'enum'
   36     1        DECLARE_CLASS                                            'color', 'enum'
   40     2        INIT_STATIC_METHOD_CALL                                  'Color', 'RED'
          3        DO_FCALL                                      0  $4      
          4        ASSIGN                                                   !0, $4
   41     5        INIT_STATIC_METHOD_CALL                                  'Color', 'RED'
          6        DO_FCALL                                      0  $6      
          7        ASSIGN                                                   !1, $6
   42     8        INIT_STATIC_METHOD_CALL                                  'Color', 'GREEN'
          9        DO_FCALL                                      0  $8      
         10        ASSIGN                                                   !2, $8
   44    11        INIT_FCALL                                               'var_dump'
         12        IS_IDENTICAL                                     ~10     !0, !1
         13        SEND_VAL                                                 ~10
         14        DO_ICALL                                                 
   45    15        INIT_FCALL                                               'var_dump'
         16        IS_EQUAL                                         ~12     !0, !1
         17        SEND_VAL                                                 ~12
         18        DO_ICALL                                                 
   46    19        INIT_FCALL                                               'var_dump'
         20        IS_IDENTICAL                                     ~14     !0, !2
         21        SEND_VAL                                                 ~14
         22        DO_ICALL                                                 
   47    23        INIT_FCALL                                               'var_dump'
         24        IS_EQUAL                                         ~16     !0, !2
         25        SEND_VAL                                                 ~16
         26        DO_ICALL                                                 
   48    27        INIT_FCALL                                               'var_dump'
         28        CAST                                          6  ~18     !0
         29        SEND_VAL                                                 ~18
         30        DO_ICALL                                                 
   49    31        INIT_FCALL                                               'var_dump'
         32        CAST                                          6  ~20     !2
         33        SEND_VAL                                                 ~20
         34        DO_ICALL                                                 
   51    35        INIT_STATIC_METHOD_CALL                                  'Color', 'GEERN'
         36        DO_FCALL                                      0  $22     
         37        ASSIGN                                                   !3, $22
         38      > RETURN                                                   1

Class Enum:
Function __construct:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 19
Branch analysis from position: 9
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 19
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/bc8OW
function name:  __construct
number of ops:  22
compiled vars:  !0 = $name
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    7     0  E >   RECV                                             !0      
    8     1        INIT_FCALL                                               'in_array'
          2        SEND_VAR                                                 !0
          3        FETCH_OBJ_R                                      ~1      'allow'
          4        SEND_VAL                                                 ~1
          5        SEND_VAL                                                 <true>
          6        DO_ICALL                                         $2      
          7        BOOL_NOT                                         ~3      $2
          8      > JMPZ                                                     ~3, ->19
    9     9    >   NEW                                              $4      'TypeError'
         10        FETCH_THIS                                       ~5      
         11        GET_CLASS                                        ~6      ~5
         12        CONCAT                                           ~7      'Undefined+enum+', ~6
         13        CONCAT                                           ~8      ~7, '%3A%3A'
         14        CONCAT                                           ~9      ~8, !0
         15        CONCAT                                           ~10     ~9, '%28%29'
         16        SEND_VAL_EX                                              ~10
         17        DO_FCALL                                      0          
         18      > THROW                                         0          $4
   11    19    >   ASSIGN_OBJ                                               'name'
         20        OP_DATA                                                  !0
   12    21      > RETURN                                                   null

End of function __construct

Function __callstatic:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 18
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
filename:       /in/bc8OW
function name:  __callStatic
number of ops:  22
compiled vars:  !0 = $name, !1 = $args, !2 = $id, !3 = $enum
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   15     2        FETCH_CLASS_NAME                                 ~4      
          3        NOP                                                      
          4        FAST_CONCAT                                      ~5      '%2C', !0
          5        CONCAT                                           ~6      ~4, ~5
          6        ASSIGN                                                   !2, ~6
   17     7        FETCH_STATIC_PROP_IS                             ~8      'enums'
          8        ISSET_ISEMPTY_DIM_OBJ                         0  ~9      ~8, !2
          9        BOOL_NOT                                         ~10     ~9
         10      > JMPZ                                                     ~10, ->18
   18    11    >   NEW                          static              $11     
         12        SEND_VAR_EX                                              !0
         13        DO_FCALL                                      0          
         14        ASSIGN                                                   !3, $11
   19    15        FETCH_STATIC_PROP_W          unknown             $14     'enums'
         16        ASSIGN_DIM                                               $14, !2
         17        OP_DATA                                                  !0
   22    18    >   FETCH_STATIC_PROP_R          unknown             ~16     'enums'
         19        FETCH_DIM_R                                      ~17     ~16, !2
         20      > RETURN                                                   ~17
   23    21*     > RETURN                                                   null

End of function __callstatic

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

End of function __tostring

End of class Enum.

Class Color: [no user functions]

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
177.75 ms | 1404 KiB | 17 Q