3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * A very edge case in PHP 5.4 regarding passing objects by reference. * It seems to be a compound effect of using reflection to set an inherited method as 'available', * AND that method taking an explicitly referential argument (&argument sig) * AND then invoking it with func_get_args() as opposed to constructing the array of args manually. * * No idea why these things all cause this behaviour or if they should. * * Important to note that this effect isn't present in PHP 5.5. */ class A { private function foo(&$arg1) { var_dump('arg1: ', $arg1); } } class B extends A { public function bar() { $x = new stdClass(); $x->baz = 'just a value'; $this->callPrivate($x); } private function callPrivate($x) { $method = new \ReflectionMethod( 'A', 'foo' ); //* for some reason, the private function needs to have been changed to be 'accessible' for this to work in 5.4 $method->setAccessible(true); //working 5.4 (* see above) but not in 5.5 $arguments = func_get_args(); //not working in either $arguments = array($x); // <---- COMMENT THIS LINE TO SEE IT WORK IN PHP 5.4 return $method->invokeArgs($this, $arguments); } } $y = new B(); $y->bar();
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/dIFVg
function name:  (null)
number of ops:  6
compiled vars:  !0 = $y
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   45     0  E >   NEW                                              $1      'B'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $1
   46     3        INIT_METHOD_CALL                                         !0, 'bar'
          4        DO_FCALL                                      0          
          5      > RETURN                                                   1

Class A:
Function foo:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/dIFVg
function name:  foo
number of ops:  6
compiled vars:  !0 = $arg1
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E >   RECV                                             !0      
   14     1        INIT_FCALL                                               'var_dump'
          2        SEND_VAL                                                 'arg1%3A+'
          3        SEND_VAR                                                 !0
          4        DO_ICALL                                                 
   15     5      > RETURN                                                   null

End of function foo

End of class A.

Class B:
Function bar:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/dIFVg
function name:  bar
number of ops:  9
compiled vars:  !0 = $x
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   20     0  E >   NEW                                              $1      'stdClass'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $1
   21     3        ASSIGN_OBJ                                               !0, 'baz'
          4        OP_DATA                                                  'just+a+value'
   22     5        INIT_METHOD_CALL                                         'callPrivate'
          6        SEND_VAR_EX                                              !0
          7        DO_FCALL                                      0          
   23     8      > RETURN                                                   null

End of function bar

Function callprivate:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/dIFVg
function name:  callPrivate
number of ops:  20
compiled vars:  !0 = $x, !1 = $method, !2 = $arguments
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   25     0  E >   RECV                                             !0      
   27     1        NEW                                              $3      'ReflectionMethod'
   28     2        SEND_VAL_EX                                              'A'
   29     3        SEND_VAL_EX                                              'foo'
          4        DO_FCALL                                      0          
   27     5        ASSIGN                                                   !1, $3
   33     6        INIT_METHOD_CALL                                         !1, 'setAccessible'
          7        SEND_VAL_EX                                              <true>
          8        DO_FCALL                                      0          
   36     9        FUNC_GET_ARGS                                    ~7      
         10        ASSIGN                                                   !2, ~7
   39    11        INIT_ARRAY                                       ~9      !0
         12        ASSIGN                                                   !2, ~9
   41    13        INIT_METHOD_CALL                                         !1, 'invokeArgs'
         14        FETCH_THIS                                       $11     
         15        SEND_VAR_EX                                              $11
         16        SEND_VAR_EX                                              !2
         17        DO_FCALL                                      0  $12     
         18      > RETURN                                                   $12
   42    19*     > RETURN                                                   null

End of function callprivate

Function foo:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/dIFVg
function name:  foo
number of ops:  6
compiled vars:  !0 = $arg1
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E >   RECV                                             !0      
   14     1        INIT_FCALL                                               'var_dump'
          2        SEND_VAL                                                 'arg1%3A+'
          3        SEND_VAR                                                 !0
          4        DO_ICALL                                                 
   15     5      > RETURN                                                   null

End of function foo

End of class B.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
155.08 ms | 1400 KiB | 15 Q