PHP: The Problem With is_callable() And __call()
Wednesday, April 22nd, 2009So I ran into this problem with PHP: when you have a class that uses method overloading via __call(), is_callable() will always return true for any function given to it, no matter if it exists or not. I know this is by design, but it’s still heavily brain-damaged. Here’s an example of what I’m talking about:
class foo {
public function __call($name, $args)
{
echo "$name called\n";
}
}
$foo = new foo();
if (is_callable(array($foo, 'non_existent_method'))) {
echo "foo->non_existant_method() is callable\n";
}
My whole problem with this, is it’s completely undocumented. If you use method_exists(), you may expect it to return true for method overloading, but that’s not the case.
So:
is_callable() - Recognizes __call()
method_exists() - Doesn’t recognize __call()
