3v4l.org

run code in 300+ PHP versions simultaneously
<?php class A {} class B {} class C {} class D {} class Z { use ConstructorParamDeprecationHelper; public function __construct( protected A $a, protected C|B $c, protected D|C $d, ) { $this->deprecateParam(B::class, 'c', func_get_args()); var_dump(get_class($this->a)); var_dump(get_class($this->c)); var_dump(get_class($this->d)); } } new Z(new A(), new B(), new C(), new D()); new Z(new A(), new C(), new D()); trait ConstructorParamDeprecationHelper { function deprecateParam($className, $paramName, $args) { if ($this->$paramName instanceof $className) { trigger_error('A, B, C, D is deprecated, pass A, C, D instead', E_USER_DEPRECATED); $constructor = new ReflectionMethod($this, '__construct'); $params = $constructor->getParameters(); $position = array_search($paramName, array_column($params, 'name')); $move = array_slice($params, $position); foreach ($move as $i => $param) { if ($constructor->getNumberOfParameters() > ($i + $position + 1)) { $next = $move[$i + 1]; $this->{$param->getName()} = $this->{$next->getName()}; } else { $this->{$param->getName()} = $args[$i + $position + 1]; } } } } }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/kWEm7
function name:  (null)
number of ops:  29
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    8     0  E >   DECLARE_CLASS                                            'z'
   22     1        NEW                                              $0      'Z'
          2        NEW                                              $1      'A'
          3        DO_FCALL                                      0          
          4        SEND_VAR_NO_REF_EX                                       $1
          5        NEW                                              $3      'B'
          6        DO_FCALL                                      0          
          7        SEND_VAR_NO_REF_EX                                       $3
          8        NEW                                              $5      'C'
          9        DO_FCALL                                      0          
         10        SEND_VAR_NO_REF_EX                                       $5
         11        NEW                                              $7      'D'
         12        DO_FCALL                                      0          
         13        SEND_VAR_NO_REF_EX                                       $7
         14        DO_FCALL                                      0          
         15        FREE                                                     $0
   23    16        NEW                                              $10     'Z'
         17        NEW                                              $11     'A'
         18        DO_FCALL                                      0          
         19        SEND_VAR_NO_REF_EX                                       $11
         20        NEW                                              $13     'C'
         21        DO_FCALL                                      0          
         22        SEND_VAR_NO_REF_EX                                       $13
         23        NEW                                              $15     'D'
         24        DO_FCALL                                      0          
         25        SEND_VAR_NO_REF_EX                                       $15
         26        DO_FCALL                                      0          
         27        FREE                                                     $10
   49    28      > RETURN                                                   1

Class A: [no user functions]
Class B: [no user functions]
Class C: [no user functions]
Class D: [no user functions]
Class Z:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/kWEm7
function name:  __construct
number of ops:  31
compiled vars:  !0 = $a, !1 = $c, !2 = $d
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
          3        ASSIGN_OBJ                                               'a'
          4        OP_DATA                                                  !0
          5        ASSIGN_OBJ                                               'c'
          6        OP_DATA                                                  !1
          7        ASSIGN_OBJ                                               'd'
          8        OP_DATA                                                  !2
   15     9        INIT_METHOD_CALL                                         'deprecateParam'
         10        SEND_VAL_EX                                              'B'
         11        SEND_VAL_EX                                              'c'
         12        FUNC_GET_ARGS                                    ~3      
         13        SEND_VAL_EX                                              ~3
         14        DO_FCALL                                      0          
   16    15        INIT_FCALL                                               'var_dump'
         16        FETCH_OBJ_R                                      ~5      'a'
         17        GET_CLASS                                        ~6      ~5
         18        SEND_VAL                                                 ~6
         19        DO_ICALL                                                 
   17    20        INIT_FCALL                                               'var_dump'
         21        FETCH_OBJ_R                                      ~8      'c'
         22        GET_CLASS                                        ~9      ~8
         23        SEND_VAL                                                 ~9
         24        DO_ICALL                                                 
   18    25        INIT_FCALL                                               'var_dump'
         26        FETCH_OBJ_R                                      ~11     'd'
         27        GET_CLASS                                        ~12     ~11
         28        SEND_VAL                                                 ~12
         29        DO_ICALL                                                 
   19    30      > RETURN                                                   null

End of function __construct

End of class Z.

Class ConstructorParamDeprecationHelper:
Function deprecateparam:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 63
Branch analysis from position: 7
2 jumps found. (Code = 77) Position 1 = 35, Position 2 = 62
Branch analysis from position: 35
2 jumps found. (Code = 78) Position 1 = 36, Position 2 = 62
Branch analysis from position: 36
2 jumps found. (Code = 43) Position 1 = 43, Position 2 = 54
Branch analysis from position: 43
1 jumps found. (Code = 42) Position 1 = 61
Branch analysis from position: 61
1 jumps found. (Code = 42) Position 1 = 35
Branch analysis from position: 35
Branch analysis from position: 54
1 jumps found. (Code = 42) Position 1 = 35
Branch analysis from position: 35
Branch analysis from position: 62
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 62
Branch analysis from position: 63
filename:       /in/kWEm7
function name:  deprecateParam
number of ops:  64
compiled vars:  !0 = $className, !1 = $paramName, !2 = $args, !3 = $constructor, !4 = $params, !5 = $position, !6 = $move, !7 = $param, !8 = $i, !9 = $next
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   26     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   27     3        FETCH_OBJ_R                                      ~10     !1
          4        FETCH_CLASS                                   0  $11     !0
          5        INSTANCEOF                                               ~10, $11
          6      > JMPZ                                                     ~12, ->63
   28     7    >   INIT_FCALL                                               'trigger_error'
          8        SEND_VAL                                                 'A%2C+B%2C+C%2C+D+is+deprecated%2C+pass+A%2C+C%2C+D+instead'
          9        SEND_VAL                                                 16384
         10        DO_ICALL                                                 
   30    11        NEW                                              $14     'ReflectionMethod'
         12        FETCH_THIS                                       $15     
         13        SEND_VAR_EX                                              $15
         14        SEND_VAL_EX                                              '__construct'
         15        DO_FCALL                                      0          
         16        ASSIGN                                                   !3, $14
   31    17        INIT_METHOD_CALL                                         !3, 'getParameters'
         18        DO_FCALL                                      0  $18     
         19        ASSIGN                                                   !4, $18
   32    20        INIT_FCALL                                               'array_search'
         21        SEND_VAR                                                 !1
         22        INIT_FCALL                                               'array_column'
         23        SEND_VAR                                                 !4
         24        SEND_VAL                                                 'name'
         25        DO_ICALL                                         $20     
         26        SEND_VAR                                                 $20
         27        DO_ICALL                                         $21     
         28        ASSIGN                                                   !5, $21
   33    29        INIT_FCALL                                               'array_slice'
         30        SEND_VAR                                                 !4
         31        SEND_VAR                                                 !5
         32        DO_ICALL                                         $23     
         33        ASSIGN                                                   !6, $23
   36    34      > FE_RESET_R                                       $25     !6, ->62
         35    > > FE_FETCH_R                                       ~26     $25, !7, ->62
         36    >   ASSIGN                                                   !8, ~26
   37    37        INIT_METHOD_CALL                                         !3, 'getNumberOfParameters'
         38        DO_FCALL                                      0  $28     
         39        ADD                                              ~29     !8, !5
         40        ADD                                              ~30     ~29, 1
         41        IS_SMALLER                                               ~30, $28
         42      > JMPZ                                                     ~31, ->54
   38    43    >   ADD                                              ~32     !8, 1
         44        FETCH_DIM_R                                      ~33     !6, ~32
         45        ASSIGN                                                   !9, ~33
   39    46        INIT_METHOD_CALL                                         !7, 'getName'
         47        DO_FCALL                                      0  $35     
         48        INIT_METHOD_CALL                                         !9, 'getName'
         49        DO_FCALL                                      0  $37     
         50        FETCH_OBJ_R                                      ~38     $37
         51        ASSIGN_OBJ                                               $35
         52        OP_DATA                                                  ~38
   37    53      > JMP                                                      ->61
   43    54    >   INIT_METHOD_CALL                                         !7, 'getName'
         55        DO_FCALL                                      0  $39     
         56        ADD                                              ~41     !8, !5
         57        ADD                                              ~42     ~41, 1
         58        FETCH_DIM_R                                      ~43     !2, ~42
         59        ASSIGN_OBJ                                               $39
         60        OP_DATA                                                  ~43
   36    61    > > JMP                                                      ->35
         62    >   FE_FREE                                                  $25
   48    63    > > RETURN                                                   null

End of function deprecateparam

End of class ConstructorParamDeprecationHelper.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
140.88 ms | 1476 KiB | 18 Q