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); return $methodReflection->getClosure($this->object)->bindTo($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(__CLASS__); } } $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 8.0.10 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
string(16) "Foo::bar#onEnter" Warning: Cannot bind method Foo::bar() to object of class Proxy in /in/8I4Tb on line 47 Fatal error: Uncaught TypeError: call_user_func_array(): Argument #1 ($callback) must be a valid callback, no array or string given in /in/8I4Tb:53 Stack trace: #0 /in/8I4Tb(104): Proxy->__call('bar', Array) #1 {main} thrown in /in/8I4Tb on line 53
Process exited with code 255.
Output for 8.0.0 - 8.0.9
string(16) "Foo::bar#onEnter" Warning: Cannot bind method Foo::bar() to object of class Proxy in /in/8I4Tb on line 47 Fatal error: Uncaught TypeError: call_user_func_array(): Argument #1 ($function) must be a valid callback, no array or string given in /in/8I4Tb:53 Stack trace: #0 /in/8I4Tb(104): Proxy->__call('bar', Array) #1 {main} thrown in /in/8I4Tb on line 53
Process exited with code 255.
Output for 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.12 - 7.3.33, 7.4.0 - 7.4.33
string(16) "Foo::bar#onEnter" Warning: Cannot bind method Foo::bar() to object of class Proxy in /in/8I4Tb on line 47 Warning: call_user_func_array() expects parameter 1 to be a valid callback, no array or string given in /in/8I4Tb on line 53 string(16) "Foo::bar#onLeave"
Output for 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.28, 7.0.0 - 7.0.20
string(16) "Foo::bar#onEnter" string(16) "Foo::qux#onEnter" string(16) "Foo::zip#onEnter" string(3) "Foo" string(16) "Foo::zip#onLeave" string(16) "Foo::qux#onLeave" string(16) "Foo::bar#onLeave"
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/8I4Tb on line 7
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected '[' in /in/8I4Tb on line 7
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/8I4Tb 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/8I4Tb 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/8I4Tb on line 6
Process exited with code 255.

preferences:
274.35 ms | 401 KiB | 357 Q