Using PHP is_callable() to Check Callable Functions and Methods
This article explains how the PHP is_callable() function can be used to determine whether a given variable, such as a function name or method name, is callable, demonstrates its usage with a complete example, and discusses its broader applications for writing more robust backend code.
In PHP, you can verify whether a function or method can be invoked by using the is_callable() function.
The is_callable() function takes a single argument—the variable to check—and returns true if the variable is callable, otherwise false .
Below is a simple example that defines a function testFunction() , assigns its name and a non‑existent method name to variables, and uses is_callable() to test each:
";
echo "methodName is callable? ";
if (is_callable($methodName)) {
echo "Yes";
} else {
echo "No";
}
?>The script outputs:
functionName is callable? Yes
methodName is callable? NoSince testFunction() exists, $functionName is callable and is_callable($functionName) returns true . The variable $methodName refers to a non‑existent method, so is_callable($methodName) returns false .
Beyond functions and methods, is_callable() can also check class constructors and static methods, making it useful for writing more robust code by verifying callability before invocation.
Overall, is_callable() is a valuable PHP function that helps developers determine whether a variable can be called, allowing them to handle potential errors gracefully.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.