3v4l.org

run code in 500+ PHP versions simultaneously
<?php class A { protected $c; protected $d; public function set(B $test) { static $testMap = []; if (empty($testMap)) { $testMap = [ C::class => 'setC', // only need to know which method to call D::class => 'setD', // ]; } if ($test === null) { return; } $testType = get_class($test); if (!array_key_exists($testType, $testMap)) { throw new InvalidArgumentException( sprintf( "Encountered unexpected body type (%s) that isn't supported", $testType ) ); } call_user_func([$this, $testMap[$testType]], $test); // uses $this to call the method on this object // alternatively, $this->{$testMap[$testType]}($test); } /** * @param C|null $c */ public function setC(C $c = null) { $this->c = $c; } /** * @param D|null $d */ public function setD(D $d = null) { $this->d = $d; } } abstract class B { protected $data; public function __construct($q = '') { $this->data = $q; } } class C extends B { } class D extends B {} $a1 = new A; $a1->set(new C("test c1")); var_dump($a1); $a2 = new A; $a2->set(new C("test c2")); var_dump($a2); $a3 = new A; $a3->set(new C("test c3")); var_dump($a3);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Hm6kS
function name:  (null)
number of ops:  37
compiled vars:  !0 = $a1, !1 = $a2, !2 = $a3
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   63     0  E >   NEW                                                  $3      'A'
          1        DO_FCALL                                          0          
          2        ASSIGN                                                       !0, $3
   64     3        INIT_METHOD_CALL                                             !0, 'set'
          4        NEW                                                  $6      'C'
          5        SEND_VAL_EX                                                  'test+c1'
          6        DO_FCALL                                          0          
          7        SEND_VAR_NO_REF_EX                                           $6
          8        DO_FCALL                                          0          
   65     9        INIT_FCALL                                                   'var_dump'
         10        SEND_VAR                                                     !0
         11        DO_ICALL                                                     
   67    12        NEW                                                  $10     'A'
         13        DO_FCALL                                          0          
         14        ASSIGN                                                       !1, $10
   68    15        INIT_METHOD_CALL                                             !1, 'set'
         16        NEW                                                  $13     'C'
         17        SEND_VAL_EX                                                  'test+c2'
         18        DO_FCALL                                          0          
         19        SEND_VAR_NO_REF_EX                                           $13
         20        DO_FCALL                                          0          
   69    21        INIT_FCALL                                                   'var_dump'
         22        SEND_VAR                                                     !1
         23        DO_ICALL                                                     
   71    24        NEW                                                  $17     'A'
         25        DO_FCALL                                          0          
         26        ASSIGN                                                       !2, $17
   72    27        INIT_METHOD_CALL                                             !2, 'set'
         28        NEW                                                  $20     'C'
         29        SEND_VAL_EX                                                  'test+c3'
         30        DO_FCALL                                          0          
         31        SEND_VAR_NO_REF_EX                                           $20
         32        DO_FCALL                                          0          
   73    33        INIT_FCALL                                                   'var_dump'
         34        SEND_VAR                                                     !2
         35        DO_ICALL                                                     
         36      > RETURN                                                       1

Class A:
Function set:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 5
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 8
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 20
Branch analysis from position: 13
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
filename:       /in/Hm6kS
function name:  set
number of ops:  28
compiled vars:  !0 = $test, !1 = $testMap, !2 = $testType
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    7     0  E >   RECV                                                 !0      
    8     1        BIND_STATIC                                                  !1
    9     2        ISSET_ISEMPTY_CV                                             !1
          3      > JMPZ                                                         ~3, ->5
   10     4    >   ASSIGN                                                       !1, <array>
   15     5    >   TYPE_CHECK                                        2          !0
          6      > JMPZ                                                         ~5, ->8
   16     7    > > RETURN                                                       null
   18     8    >   GET_CLASS                                            ~6      !0
          9        ASSIGN                                                       !2, ~6
   19    10        ARRAY_KEY_EXISTS                                     ~8      !2, !1
         11        BOOL_NOT                                             ~9      ~8
         12      > JMPZ                                                         ~9, ->20
   20    13    >   NEW                                                  $10     'InvalidArgumentException'
   23    14        ROPE_INIT                                         3  ~12     'Encountered+unexpected+body+type+%28'
         15        ROPE_ADD                                          1  ~12     ~12, !2
         16        ROPE_END                                          2  ~11     ~12, '%29+that+isn%27t+supported'
         17        SEND_VAL_EX                                                  ~11
   20    18        DO_FCALL                                          0          
   23    19      > THROW                                             0          $10
   27    20    >   FETCH_THIS                                           ~15     
         21        INIT_ARRAY                                           ~16     ~15
         22        FETCH_DIM_R                                          ~17     !1, !2
         23        ADD_ARRAY_ELEMENT                                    ~16     ~17
         24        INIT_USER_CALL                                    1          'call_user_func', ~16
         25        SEND_USER                                                    !0
         26        DO_FCALL                                          0          
   29    27      > RETURN                                                       null

End of function set

Function setc:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Hm6kS
function name:  setC
number of ops:  4
compiled vars:  !0 = $c
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   34     0  E >   RECV_INIT                                            !0      null
   36     1        ASSIGN_OBJ                                                   'c'
          2        OP_DATA                                                      !0
   37     3      > RETURN                                                       null

End of function setc

Function setd:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Hm6kS
function name:  setD
number of ops:  4
compiled vars:  !0 = $d
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   42     0  E >   RECV_INIT                                            !0      null
   44     1        ASSIGN_OBJ                                                   'd'
          2        OP_DATA                                                      !0
   45     3      > RETURN                                                       null

End of function setd

End of class A.

Class B:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Hm6kS
function name:  __construct
number of ops:  4
compiled vars:  !0 = $q
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   51     0  E >   RECV_INIT                                            !0      ''
   53     1        ASSIGN_OBJ                                                   'data'
          2        OP_DATA                                                      !0
   54     3      > RETURN                                                       null

End of function __construct

End of class B.

Class C:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Hm6kS
function name:  __construct
number of ops:  4
compiled vars:  !0 = $q
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   51     0  E >   RECV_INIT                                            !0      ''
   53     1        ASSIGN_OBJ                                                   'data'
          2        OP_DATA                                                      !0
   54     3      > RETURN                                                       null

End of function __construct

End of class C.

Class D:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Hm6kS
function name:  __construct
number of ops:  4
compiled vars:  !0 = $q
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   51     0  E >   RECV_INIT                                            !0      ''
   53     1        ASSIGN_OBJ                                                   'data'
          2        OP_DATA                                                      !0
   54     3      > RETURN                                                       null

End of function __construct

End of class D.

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
152.19 ms | 1635 KiB | 9 Q