I am trying to figure out how to catch any method called on an object in PHP. I know about the magic function __call
, but it is triggered only for methods that
Taking your original Foo
implementation you could wrap a decorator
around it like this:
class Foo
{
public function bar() {
echo 'foobar';
}
}
class Decorator
{
protected $foo;
public function __construct(Foo $foo) {
$this->foo = $foo;
}
public function __call($method_name, $args) {
echo 'Calling method ',$method_name,'
';
return call_user_func_array(array($this->foo, $method_name), $args);
}
}
$foo = new Decorator(new Foo());
$foo->bar();