3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?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 7.1.0
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 - 7.0.14
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.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.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.