<?php
class Proxy
{
private $object;
private $factoryArray = [];
public function __construct($object, array $factoryArray = [])
{
$this->object = $object;
$this->factoryArray = $factoryArray;
}
public function __call($name, array $argumentArray)
{
$reflection = new ReflectionMethod($this->object, $name);
$function = $reflection->getClosure($this->object)->bindTo($this, null);
return call_user_func_array($function, $argumentArray);
}
public function __get($name)
{
if ($name[0] == '!')
{
return $this->__getFactory(substr($name, 1));
}
}
public function __getFactory($name)
{
if (isset($this->factoryArray[$name]))
{
return new self($this->factoryArray[$name](), $this->factoryArray);
}
}
}
class Foo
{
public function doStuff1()
{
$this->{'!Bar'}->doStuff2();
}
}
class Bar
{
public function doStuff2()
{
$this->{'!Qux'}->doStuff3();
}
}
class Qux
{
public function doStuff3()
{
var_dump(__METHOD__);
}
}
$foo = new Proxy(new Foo, [
'Bar' => function () {
return new Bar;
},
'Qux' => function () {
return new Qux;
},
]);
$foo->doStuff1();
Warning: Cannot bind method Foo::doStuff1() to object of class Proxy in /in/UXibA on line 19
Fatal error: Uncaught TypeError: call_user_func_array(): Argument #1 ($callback) must be a valid callback, no array or string given in /in/UXibA:20
Stack trace:
#0 /in/UXibA(80): Proxy->__call('doStuff1', Array)
#1 {main}
thrown in /in/UXibA on line 20
Process exited with code 255.
Output for 8.0.0 - 8.0.9
Warning: Cannot bind method Foo::doStuff1() to object of class Proxy in /in/UXibA on line 19
Fatal error: Uncaught TypeError: call_user_func_array(): Argument #1 ($function) must be a valid callback, no array or string given in /in/UXibA:20
Stack trace:
#0 /in/UXibA(80): Proxy->__call('doStuff1', Array)
#1 {main}
thrown in /in/UXibA on line 20
Process exited with code 255.
Warning: Cannot bind method Foo::doStuff1() to object of class Proxy in /in/UXibA on line 19
Warning: call_user_func_array() expects parameter 1 to be a valid callback, no array or string given in /in/UXibA on line 20
Output for 7.0.0 - 7.0.20
Warning: Cannot rebind scope of closure created by ReflectionFunctionAbstract::getClosure() in /in/UXibA on line 19
Warning: call_user_func_array() expects parameter 1 to be a valid callback, no array or string given in /in/UXibA on line 20
Parse error: syntax error, unexpected '[' in /in/UXibA on line 8
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected '[' in /in/UXibA on line 8
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/UXibA on line 6
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/UXibA on line 6
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/UXibA on line 6
Process exited with code 255.