我在几个类的包装类上使用php __call。
class Wrapper {
public function __construct($obj) {...}
public function __call($methodName, $params) {
$this->preprocessSomeStuff();
// call the original object's method
$sourceMethod = new ReflectionMethod($this->obj, $methodName);
return $sourceMethod->invokeArgs($this->obj, $params);
}
这适用于初始调用,但如果$ obj具有嵌套方法调用,则不起作用,例如
class Thing {
public function doStuff() {
$this->doMoreStuff();
}
public function doMoreStuff() {...}
}
使用此对象,调用:
$wrappedThing = new Wrapper(new Thing());
// preprocessing applied for doStuff(), not applied for doMoreStuff()
$wrappedThing->doStuff();