3v4l.org

run code in 300+ PHP versions simultaneously
<?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 8.0.10 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
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.
Output for 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33
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
Output for 5.4.0 - 5.4.45, 5.5.24 - 5.5.35, 5.6.7 - 5.6.28
string(13) "Qux::doStuff3"
Output for 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.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.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/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.

preferences:
262.04 ms | 401 KiB | 357 Q