ubu*_*gnu 2 php oop magic-methods
PHP中是否有一个魔术方法,每次调用另一个方法时都会调用它?__callStatic()和__call()魔术方法似乎只在方法不存在时才起作用.
不,你所要求的是"建议"或面向方面的编程.我相信有一些实验扩展可以在PHP中实现这一点,但是你不想在生产中使用它.
你可以做的是创建一个包装器,它通过代理所有的调用__call.例如:
class Foo {
function bar() {
echo "Foo::bar\n";
}
}
class AdviceWrapper {
protected $subject;
function __construct($subject) {
$this->subject = $subject;
}
function __call($name, $args) {
echo "Before $name\n";
$result = call_user_func_array(array($this->subject, $name), $args);
echo "After $name\n";
return $result;
}
}
$foo = new AdviceWrapper(new Foo());
$foo->bar();
Run Code Online (Sandbox Code Playgroud)
另见:https://stackoverflow.com/questions/4738282/are-there-any-working-aspect-oriented-php-libraries