Backend Development 3 min read

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.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using PHP is_callable() to Check Callable Functions and Methods

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? No

Since 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.

backend developmentis_callablecallable checkfunction existence
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.