3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?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();
Output for 7.1.0
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.14
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
Output for 5.4.0 - 5.6.28
string(13) "Qux::doStuff3"
Output for 5.1.0 - 5.3.29
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.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
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.