3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Proxy { private $object; private $onEnter = []; private $onLeave = []; public function __registerOnEnter($method, \Closure $closure) { $this->onEnter[$method][] = $closure; } private function invokeOnEnter($method, array $arguments) { if (empty($this->onEnter[$method])) { return; } foreach ($this->onEnter[$method] as $closure) { call_user_func($closure, $this->object, $arguments); } } public function __registerOnLeave($method, \Closure $closure) { $this->onLeave[$method][] = $closure; } private function invokeOnLeave($method, array $arguments) { if (empty($this->onLeave[$method])) { return; } foreach ($this->onLeave[$method] as $closure) { call_user_func($closure, $this->object, $arguments); } } private function getProxiedMethod($method) { $methodReflection = new \ReflectionMethod($this->object, $method); $closure = $methodReflection->getClosure($this->object); return \Closure::bind($closure, $this); } public function __call($method, array $arguments) { $this->invokeOnEnter($method, $arguments); call_user_func_array($this->getProxiedMethod($method), $arguments); $this->invokeOnLeave($method, $arguments); } public function __construct($object) { $this->object = (object) $object; } } class Foo { public function bar() { $this->qux(); } public function qux() { $this->zip(); } public function zip() { var_dump(get_class($this)); } } $fooProxy = new Proxy(new Foo()); $fooProxy->__registerOnEnter('bar', function ($object) { var_dump(sprintf('%s::bar#onEnter', get_class($object))); }); $fooProxy->__registerOnLeave('bar', function ($object) { var_dump(sprintf('%s::bar#onLeave', get_class($object))); }); $fooProxy->__registerOnEnter('qux', function ($object) { var_dump(sprintf('%s::qux#onEnter', get_class($object))); }); $fooProxy->__registerOnLeave('qux', function ($object) { var_dump(sprintf('%s::qux#onLeave', get_class($object))); }); $fooProxy->__registerOnEnter('zip', function ($object) { var_dump(sprintf('%s::zip#onEnter', get_class($object))); }); $fooProxy->__registerOnLeave('zip', function ($object) { var_dump(sprintf('%s::zip#onLeave', get_class($object))); }); $fooProxy->bar();
Output for git.master, git.master_jit, rfc.property-hooks
string(16) "Foo::bar#onEnter" Warning: Cannot bind method Foo::bar() to object of class Proxy in /in/DooXD on line 48 Fatal error: Uncaught TypeError: call_user_func_array(): Argument #1 ($callback) must be a valid callback, no array or string given in /in/DooXD:54 Stack trace: #0 /in/DooXD(105): Proxy->__call('bar', Array) #1 {main} thrown in /in/DooXD on line 54
Process exited with code 255.

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
46.88 ms | 401 KiB | 8 Q