dimanche 8 février 2015

Using __call to preprocess method calls to a class. How do I apply this for nested method calls?

I'm using the php __call on a wrapper class for several classes.



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);
}


This works great for the initial call, but doesn't work if the $obj has a nested method call, e.g.



class Thing {
public function doStuff() {
$this->doMoreStuff();
}

public function doMoreStuff() {...}
}


With this object, calling:



$wrappedThing = new Wrapper(new Thing());
// preprocessing applied for doStuff(), not applied for doMoreStuff()
$wrappedThing->doStuff();





Aucun commentaire:

Enregistrer un commentaire