<?php
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);
}
}
$obj = new B;
$obj->bar();
Warning: A::foo(): Argument #1 ($arg1) must be passed by reference, value given in /in/YI5G3 on line 31
string(6) "arg1: "
object(stdClass)#2 (1) {
["baz"]=>
string(12) "just a value"
}
Warning: Parameter 1 to A::foo() expected to be a reference, value given in /in/YI5G3 on line 31
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/YI5G3 on line 31
Fatal error: Uncaught ReflectionException: Invocation of method A::foo() failed in /in/YI5G3:31
Stack trace:
#0 /in/YI5G3(31): ReflectionMethod->invokeArgs(Object(B), Array)
#1 /in/YI5G3(12): B->callPrivate(Object(stdClass))
#2 /in/YI5G3(35): B->bar()
#3 {main}
thrown in /in/YI5G3 on line 31
Process exited with code 255.
Output for 5.5.6 - 5.5.7, 5.6.0 - 5.6.28
Warning: Parameter 1 to A::foo() expected to be a reference, value given in /in/YI5G3 on line 31
Fatal error: Uncaught exception 'ReflectionException' with message 'Invocation of method A::foo() failed' in /in/YI5G3:31
Stack trace:
#0 /in/YI5G3(31): ReflectionMethod->invokeArgs(Object(B), Array)
#1 /in/YI5G3(12): B->callPrivate(Object(stdClass))
#2 /in/YI5G3(35): B->bar()
#3 {main}
thrown in /in/YI5G3 on line 31
Process exited with code 255.
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/YI5G3 on line 17
Fatal error: Call to undefined method ReflectionMethod::setAccessible() in /in/YI5G3 on line 23
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/YI5G3 on line 3
Process exited with code 255.
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/YI5G3 on line 3
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/YI5G3 on line 3
Process exited with code 255.