PHP is_callable() Function
The is_callable() function is used to verify whether a function can be called in the current environment.
The is_callable() function checks if the contents of a variable can be called as a function. This can verify a variable containing a valid function name or an array containing an object and a method name.
PHP Version Requirements: PHP 4 >= 4.0.6, PHP 5, PHP 7
Syntax
bool is_callable ( callable $name [, bool $syntax_only = false [, string &$callable_name ]] )
Parameter Descriptions:
$name: The callback function to check.
$syntax_only: If set to TRUE, this function only verifies if the name might be a function or method. It rejects non-strings or structures that do not have a valid format for a callback. A valid structure should contain two elements, the first being an object or string, and the second being a string.
$callable_name: Accepts the "callable name".
Return Value
Returns TRUE if the name is callable, otherwise FALSE.
Example
<?php
// Check if a variable is a callable function
function someFunction()
{
}
$functionVariable = 'someFunction';
var_dump(is_callable($functionVariable, false, $callable_name)); // bool(true)
echo $callable_name, "\n"; // someFunction
//
// Array containing a method
//
class someClass {
function someMethod()
{
}
}
$anObject = new someClass();
$methodVariable = array($anObject, 'someMethod');
var_dump(is_callable($methodVariable, true, $callable_name)); // bool(true)
echo $callable_name, "\n"; // someClass::someMethod
?>
Output:
bool(true)
someFunction
bool(true)
someClass::someMethod