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();

preferences:
32.46 ms | 402 KiB | 5 Q