The only other way, would be to create a generic "superclass"...
class SuperClass {
protected $obj = null;
protected $overrides = array();
public function __construct($obj) {
if (!is_object($obj)) {
throw new InvalidArgumentException('Argument is not an object');
}
$this->obj = $obj;
}
public function __call($method, $args) {
$method = strtolower($method);
if (isset($this->overrides[$method])) {
array_unshift($args, $this);
return call_user_func_array($this->overrides[$method], $args);
} elseif (is_callable(array($this->obj, $method))) {
return call_user_func_array(array($this->obj, $method), $args);
} else {
throw new BadMethodCallException('Invalid Method Called');
}
}
public function __get($var) {
return isset($this->obj->$var) ? $this->obj->$var : null;
}
public function __set($var, $value) {
$this->obj->$var = $value;
}
public function addOverride($method, $callback) {
$this->overrides[strtolower($method)] = $callback;
}
}
It's not always the best solution, but it's possible that some situations exist to use something like that. It will let you "add" and "override" methods to any object at run time.
The better generic solution is to simply extend the class in a child class... But the above "superclass" does have some uses...