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();
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Warning: A::foo(): Argument #1 ($arg1) must be passed by reference, value given in /in/GUuZL on line 41 string(6) "arg1: " object(stdClass)#2 (1) { ["baz"]=> string(12) "just a value" }
Output for 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.12 - 7.3.33, 7.4.0 - 7.4.33
Warning: Parameter 1 to A::foo() expected to be a reference, value given in /in/GUuZL on line 41 string(6) "arg1: " object(stdClass)#2 (1) { ["baz"]=> string(12) "just a value" }
Output for 7.0.0 - 7.0.20
Warning: Parameter 1 to A::foo() expected to be a reference, value given in /in/GUuZL on line 41 Fatal error: Uncaught ReflectionException: Invocation of method A::foo() failed in /in/GUuZL:41 Stack trace: #0 /in/GUuZL(41): ReflectionMethod->invokeArgs(Object(B), Array) #1 /in/GUuZL(22): B->callPrivate(Object(stdClass)) #2 /in/GUuZL(46): B->bar() #3 {main} thrown in /in/GUuZL on line 41
Process exited with code 255.
Output for 5.6.7 - 5.6.28
Warning: Parameter 1 to A::foo() expected to be a reference, value given in /in/GUuZL on line 41 Fatal error: Uncaught exception 'ReflectionException' with message 'Invocation of method A::foo() failed' in /in/GUuZL:41 Stack trace: #0 /in/GUuZL(41): ReflectionMethod->invokeArgs(Object(B), Array) #1 /in/GUuZL(22): B->callPrivate(Object(stdClass)) #2 /in/GUuZL(46): B->bar() #3 {main} thrown in /in/GUuZL on line 41
Process exited with code 255.
Output for 5.3.2 - 5.3.29, 5.4.0 - 5.4.45, 5.5.24 - 5.5.35
string(6) "arg1: " object(stdClass)#2 (1) { ["baz"]=> string(12) "just a value" }
Output for 5.3.0 - 5.3.1
Fatal error: Call to undefined method ReflectionMethod::setAccessible() in /in/GUuZL on line 33
Process exited with code 255.
Output for 5.0.0 - 5.0.5, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/GUuZL on line 27 Fatal error: Call to undefined method ReflectionMethod::setAccessible() in /in/GUuZL on line 33
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/GUuZL on line 13
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/GUuZL on line 13
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/GUuZL on line 13
Process exited with code 255.

preferences:
231.68 ms | 401 KiB | 314 Q