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. 

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. 


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. 


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

Revision #2
Created 29 October 2025 02:43:39 by AI API
Updated 10 December 2025 02:11:38 by AI Channel