Verify that the contents of a variable can be called as a function
Syntax
is_callable(mixed $value, bool $syntax_only = false, string &$callable_name = null): bool
Parameters
value
The value to check
syntax_only
If set to true the function only verifies that value might be a function or method. It will only reject simple variables that are not strings, or an array that does not have a valid structure to be used as a callback. The valid ones are supposed to have only 2 entries, the first of which is an object or a string, and the second a string.
callable_name
Receives the "callable name".
Return
Returns true if value is callable, false otherwise.
Examples
1 · value · Function
<? function myfunction() { } $value = "myfunction"; $return = is_callable($value); var_export($return); ?>
true
2 · value · Method
<? class myclass { function myfunction() { } } $value = array(new myclass, "myfunction"); $return = is_callable($value); var_export($return); ?>
true
3 · syntax_only · Function
<? function myfunction() { } $value = "myfunction"; $syntax_only = true; $return = is_callable($value, $syntax_only); var_export($return); ?>
true
4 · syntax_only · Method
<? class myclass { function myfunction() { } } $value = array(new myclass, "myfunction"); $syntax_only = true; $return = is_callable($value, $syntax_only); var_export($return); ?>
true
5 · callable_name · Function
<? function myfunction() { } $value = "myfunction"; $syntax_only = true; $return = is_callable($value, $syntax_only, $callable_name); var_export($return); echo PHP_EOL . $callable_name; ?>
true myfunction
6 · callable_name · Method
<? class myclass { function myfunction() { } } $value = array(new myclass, "myfunction"); $syntax_only = true; $return = is_callable($value, $syntax_only, $callable_name); var_export($return); echo PHP_EOL . $callable_name; ?>
true myclass::myfunction