@ 2013-10-24T15:08:23Z <?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();
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
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/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 . 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:dark mode live preview
44.22 ms | 401 KiB | 8 Q