I'm writing a class that uses __call()
to handle some magic functions, with no parent class, I can't find a syntax that allows me to pass responsibility on to methods not handled by my __call()
implementation:
call_user_func_array([$this, $methodCalled], $arguments) //- infinite loop
call_user_method_array($methodCalled, $this, $arguments) //- infinite loop
parent::__call($methodCalled, $arguments) //- doesn't work if I don't *have* a parent (or even if I do have a parent, if that parent doesn't define __call())
I've run out of ideas for how to handle this case and google is not being my friend :-(
I think you want something like:
public function __call($method, $args) {
switch($method) {
case "MethodA":
// handle it
break;
case "MethodB":
// handle it
break;
default:
$parent = get_parent_class();
if ($parent && (method_exists($parent, $method) || method_exists($parent, '__call')))
return parent::__call($method, $args);
else
trigger_error("Call to unhandled __call function $method()", E_USER_ERROR);
}
}
You can place it in child and parent classes. If a call is left unhandled in a child, the child will look if its parent can handle it and otherwise trigger an error.