3v4l.org

run code in 300+ PHP versions simultaneously
<?php class MyEnum { private $values = [ 'A' => 1, 'B' => 2, 'C' => 3, ]; private $key = null; public function __construct($key) { if (!isset($this->values[$key])) { throw new Exception('Incorrect Enum Value'); } $this->key = $key; } public function key(): string { return $this->key; } public function val(): int { return $this->values[$this->key]; } } function foo(MyEnum $foo) { echo $foo->val().PHP_EOL; } // This works foo(new MyEnum('A')); try { // Rejected, don't allow passing the underlying value foo(1); } catch (\Throwable $e) { echo $e->getMessage().PHP_EOL; } try { // Rejected, value not part of the enum foo(new MyEnum('D')); } catch (\Throwable $e) { echo $e->getMessage().PHP_EOL; }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 15
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 27
Branch analysis from position: 27
1 jumps found. (Code = 62) Position 1 = -2
Found catch point at position: 10
Branch analysis from position: 10
2 jumps found. (Code = 107) Position 1 = 11, Position 2 = -2
Branch analysis from position: 11
1 jumps found. (Code = 42) Position 1 = 27
Branch analysis from position: 27
Found catch point at position: 22
Branch analysis from position: 22
2 jumps found. (Code = 107) Position 1 = 23, Position 2 = -2
Branch analysis from position: 23
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/bBpRu
function name:  (null)
number of ops:  28
compiled vars:  !0 = $e
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   35     0  E >   INIT_FCALL                                               'foo'
          1        NEW                                              $1      'MyEnum'
          2        SEND_VAL_EX                                              'A'
          3        DO_FCALL                                      0          
          4        SEND_VAR                                                 $1
          5        DO_FCALL                                      0          
   39     6        INIT_FCALL                                               'foo'
          7        SEND_VAL                                                 1
          8        DO_FCALL                                      0          
          9      > JMP                                                      ->15
   40    10  E > > CATCH                                       last         'Throwable'
   41    11    >   INIT_METHOD_CALL                                         !0, 'getMessage'
         12        DO_FCALL                                      0  $5      
         13        CONCAT                                           ~6      $5, '%0A'
         14        ECHO                                                     ~6
   46    15    >   INIT_FCALL                                               'foo'
         16        NEW                                              $7      'MyEnum'
         17        SEND_VAL_EX                                              'D'
         18        DO_FCALL                                      0          
         19        SEND_VAR                                                 $7
         20        DO_FCALL                                      0          
         21      > JMP                                                      ->27
   47    22  E > > CATCH                                       last         'Throwable'
   48    23    >   INIT_METHOD_CALL                                         !0, 'getMessage'
         24        DO_FCALL                                      0  $10     
         25        CONCAT                                           ~11     $10, '%0A'
         26        ECHO                                                     ~11
   49    27    > > RETURN                                                   1

Function foo:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/bBpRu
function name:  foo
number of ops:  6
compiled vars:  !0 = $foo
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   30     0  E >   RECV                                             !0      
   31     1        INIT_METHOD_CALL                                         !0, 'val'
          2        DO_FCALL                                      0  $1      
          3        CONCAT                                           ~2      $1, '%0A'
          4        ECHO                                                     ~2
   32     5      > RETURN                                                   null

End of function foo

Class MyEnum:
Function __construct:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 9
Branch analysis from position: 5
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/bBpRu
function name:  __construct
number of ops:  12
compiled vars:  !0 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E >   RECV                                             !0      
   14     1        FETCH_OBJ_IS                                     ~1      'values'
          2        ISSET_ISEMPTY_DIM_OBJ                         0  ~2      ~1, !0
          3        BOOL_NOT                                         ~3      ~2
          4      > JMPZ                                                     ~3, ->9
   15     5    >   NEW                                              $4      'Exception'
          6        SEND_VAL_EX                                              'Incorrect+Enum+Value'
          7        DO_FCALL                                      0          
          8      > THROW                                         0          $4
   18     9    >   ASSIGN_OBJ                                               'key'
         10        OP_DATA                                                  !0
   19    11      > RETURN                                                   null

End of function __construct

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

End of function key

Function val:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/bBpRu
function name:  val
number of ops:  7
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   26     0  E >   FETCH_OBJ_R                                      ~1      'key'
          1        FETCH_OBJ_R                                      ~0      'values'
          2        FETCH_DIM_R                                      ~2      ~0, ~1
          3        VERIFY_RETURN_TYPE                                       ~2
          4      > RETURN                                                   ~2
   27     5*       VERIFY_RETURN_TYPE                                       
          6*     > RETURN                                                   null

End of function val

End of class MyEnum.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
154.58 ms | 1403 KiB | 16 Q