3v4l.org

run code in 500+ 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, NULL, TRUE); foreach ($move as $i => $param) { $this->{$param->getName()} = $args[$i + 1]; } } } }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/MtHNJ
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
   40    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/MtHNJ
function name:  __construct
number of ops:  31
compiled vars:  !0 = $a, !1 = $c, !2 = $d
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   11     0  E >   RECV                                                 !0      
   12     1        RECV                                                 !1      
   13     2        RECV                                                 !2      
   11     3        ASSIGN_OBJ                                                   'a'
          4        OP_DATA                                                      !0
   12     5        ASSIGN_OBJ                                                   'c'
          6        OP_DATA                                                      !1
   13     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 = 47
Branch analysis from position: 7
2 jumps found. (Code = 77) Position 1 = 37, Position 2 = 46
Branch analysis from position: 37
2 jumps found. (Code = 78) Position 1 = 38, Position 2 = 46
Branch analysis from position: 38
1 jumps found. (Code = 42) Position 1 = 37
Branch analysis from position: 37
Branch analysis from position: 46
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 46
Branch analysis from position: 47
filename:       /in/MtHNJ
function name:  deprecateParam
number of ops:  48
compiled vars:  !0 = $className, !1 = $paramName, !2 = $args, !3 = $constructor, !4 = $params, !5 = $position, !6 = $move, !7 = $param, !8 = $i
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                                          ~9      !1
          4        FETCH_CLASS                                       0  $10     !0
          5        INSTANCEOF                                                   ~9, $10
          6      > JMPZ                                                         ~11, ->47
   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                                                  $13     'ReflectionMethod'
         12        FETCH_THIS                                           $14     
         13        SEND_VAR_EX                                                  $14
         14        SEND_VAL_EX                                                  '__construct'
         15        DO_FCALL                                          0          
         16        ASSIGN                                                       !3, $13
   31    17        INIT_METHOD_CALL                                             !3, 'getParameters'
         18        DO_FCALL                                          0  $17     
         19        ASSIGN                                                       !4, $17
   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                                             $19     
         26        SEND_VAR                                                     $19
         27        DO_ICALL                                             $20     
         28        ASSIGN                                                       !5, $20
   33    29        INIT_FCALL                                                   'array_slice'
         30        SEND_VAR                                                     !4
         31        SEND_VAR                                                     !5
         32        SEND_VAL                                                     null
         33        SEND_VAL                                                     <true>
         34        DO_ICALL                                             $22     
         35        ASSIGN                                                       !6, $22
   35    36      > FE_RESET_R                                           $24     !6, ->46
         37    > > FE_FETCH_R                                           ~25     $24, !7, ->46
         38    >   ASSIGN                                                       !8, ~25
   36    39        INIT_METHOD_CALL                                             !7, 'getName'
         40        DO_FCALL                                          0  $27     
         41        ADD                                                  ~29     !8, 1
         42        FETCH_DIM_R                                          ~30     !2, ~29
         43        ASSIGN_OBJ                                                   $27
         44        OP_DATA                                                      ~30
   35    45      > JMP                                                          ->37
         46    >   FE_FREE                                                      $24
   39    47    > > RETURN                                                       null

End of function deprecateparam

End of class ConstructorParamDeprecationHelper.

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
161.33 ms | 1801 KiB | 18 Q