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:

<?php

function testFunction() {
  echo "Hello, world!";
}

$functionName = 'testFunction';
$methodName   = 'nonExistent';

echo "functionName is callable? ";
if (is_callable($functionName)) {
  echo "Yes";
} else {
  echo "No";
}

echo "<br>";

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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PHPis_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

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.