What exactly is the difference between the is_callable and function_exists in PHP?

The function is_callable accepts not only function names, but also other types of callbacks:

  • Foo::method
  • array("Foo", "method")
  • array($obj, "method")
  • Closures and other invokable objects (PHP 5.3)

So is_callable accepts anything that you could pass call_user_func and family, while function_exists only tells if a certain function exists (not methods, see method_exists for that, nor closures).

Put another way, is_callable is a wrapper for zend_is_callable, which handles variables with the pseudo-type callback, while function_exists only does a hash table lookup in the functions' table.


When used with a function (not a class method) there is no difference except that function_exists is slightly faster.

But when used to check the existence of methods in a class you cannot use function_exists. You'll have to use is_callable or method_exists.


When used in class context, is_callable returns true for class methods that are accessible ie public methods but method_exists returns true for all methods - public, protected and private. function_exists does same thing as method_exists outside class contexts.

Tags:

Php