PHP __call vs method_exists
__call
handles calls to methods that don't exist. method_exists
is an introspection method that checks the existence of a method.
How can __call
be determined to handle a method? I think you have to throw an exception manually in __call
if doesn't handle your request and catch the exception in the code that would otherwise use method_exists
. BadMethodCallException
exists for this purpose.
Have a look at is_callable()
.
But no, if the __call()
method only handles some names, then you would need some other way of checking if the call will succeed.
Might I suggest a interface with the method canCall($function)
, or something? Then check if the class implements the interface. If it doesn't, just use is_callable()
.
method_exists tries two things:
- Searches for the method name in the class's function table. Those are the
function foo() {}
type methods. - Checks if the class (the C code) has a (C code)
get_method()
function and if it has invoke it to let the class implementation decide.
You'd need the latter. But this get_method()
is not "extended" to the php script code, i.e. there is no way to let get_method() call some user defined php script code (And what would this php code return?).
So the answer to my best knowledge is: No, it's not possible (yet?).
The implementation of ZEND_FUNCTION(method_exists)
can be found in zend/zend_builtin_functions.c
and is I think fairly readable even if you don't know C but PHP.