PHP is_callable() Function
is_callable()This function is used to detect whether a function is callable in the current environment.
is_callable()This function verifies whether the contents of a variable can be called as a function. This can check a variable containing a valid function name, or an array containing a properly encoded object and a function 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 Description:
- $name: The callback function to check.
- $syntax_only: If set to TRUE, the function only verifies that name may be a function or method. It only rejects non-strings, or structures that do not contain a valid structure that can be used as a callback function. A valid one should contain two elements: the first is an object or a string, and the second element is a string.
- $callable_name: Accepts a "callable name".
Return Value
Returns TRUE if name is callable, otherwise returns FALSE.
Example
Example
//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
The output is:
bool(true) someFunction bool(true) someClass::someMethodOther Extensions
Available PHP Functions