Skip to main content

is_callable(), function_exists(), and method_exists() in PHP

In PHP,

is_callable(), function_exists(), and method_exists() are functions used to verify the existence or usability of functions and methods, but they serve different, specific purposes. 



function_exists()

This function checks if a general standalone function (either built-in or user-defined) has been defined. It takes the function name as a string argument. 

  • Purpose: To prevent errors that occur when calling a function that is not available (e.g., due to specific PHP configurations or missing extension).
  • Syntax: function_exists(string $function_name): bool
  • Example:
    php
if (function_exists('curl_init')) {
    echo "cURL functions are available.";
} else {
    echo "cURL functions are not available.";
}

 


method_exists()

This function checks if a specific method exists within a given class or object. It only verifies the definition of the method in the class's function table, not whether it can actually be called in the current context. 

  • Purpose: Useful for checking if a method is defined before attempting to call it, particularly when dealing with dynamic method calls or abstract classes.
  • Syntax: method_exists(mixed $object_or_class, string $method_name): bool
  • Example:
    php
  • class MyClass {
        public function myMethod() {}
    }
    $obj = new MyClass();
    
    if (method_exists($obj, 'myMethod')) {
        echo "Method exists.";
    }
    
  • Note: This function returns false for methods handled by the __call() magic method unless they are explicitly defined. 

is_callable()

This is the most general function, verifying whether the contents of a variable can actually be called as a function from the current scope. This includes standalone functions, static class methods, object methods, and even objects that implement the __invoke() magic method. 

  • Purpose: Used when you need to ensure a given callback is valid before using it with functions like call_user_func() or array_map().
  • Syntax: is_callable(mixed $var, bool $syntax_only = false, string &$callable_name = null): bool
  • Example:
    php
  • class MyClass {
        public function myMethod() {}
    }
    $obj = new MyClass();
    
    if (is_callable(array($obj, 'myMethod'))) {
        echo "The method can be called.";
    }
    
    // Also works for simple functions
    $functionName = 'function_exists';
    if (is_callable($functionName)) {
        echo "$functionName is callable.";
    }
    
  • Note: is_callable() is more robust than method_exists() for determining actual invocability because it respects scope and visibility, but it can return true for methods that don't technically "exist" if a class uses the __call() or __callStatic() magic methods. 

Summary of Differences


Function Checks for Scope Handles Magic Methods?
function_exists() Standalone function definition Global scope No
method_exists() Method definition in a class/object Class scope (public, protected, private) No (returns false for methods handled by __call())
is_callable() Whether something can be invoked Current scope and visibility Yes (__call(), __invoke(), __callStatic())
In general, use function_exists() for global functions, method_exists() for checking if a method is explicitly defined in a class, and is_callable() when you intend to immediately execute a callback and need to verify it is possible to do so