is_callable
(PHP 4 >= 4.0.6, PHP 5)
is_callable — Verify that the contents of a variable can be called as a function
Description
Verify that the contents of a variable can be called as a function. This can check that a simple variable contains the name of a valid function, or that an array contains a properly encoded object and function name.
Parameters
-
name
-
The callback function to check
-
syntax_only
-
If set to
TRUE
the function only verifies thatname
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". In the example below it is "someClass::someMethod". Note, however, that despite the implication that someClass::SomeMethod() is a callable static method, this is not the case.
Return Values
Returns TRUE
if name
is callable, FALSE
otherwise.
Examples
Example #1 is_callable() example
<?php
// How to check a variable to see if it can be called
// as a function.
//
// Simple variable containing a 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
?>
See Also
- function_exists() - Return TRUE if the given function has been defined
- method_exists() - Checks if the class method exists
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Расширения, относящиеся к переменным и типам
- Функции для работы с переменными
- boolval
- debug_zval_dump
- doubleval
- empty
- floatval
- get_defined_vars
- get_resource_type
- gettype
- import_request_variables
- intval
- is_array
- is_bool
- is_callable
- is_double
- is_float
- is_int
- is_integer
- is_long
- is_null
- is_numeric
- is_object
- is_real
- is_resource
- is_scalar
- is_string
- isset
- print_r
- serialize
- settype
- strval
- unserialize
- unset
- var_dump
- var_export
Коментарии
If the target class has __call() magic function implemented, then is_callable will ALWAYS return TRUE for whatever method you call it.
is_callable does not evaluate your internal logic inside __call() implementation (and this is for good).
Therefore every method name is callable for such classes.
Hence it is WRONG to say (as someone said):
...is_callable will correctly determine the existence of methods made with __call...
Example:
<?php
class TestCallable
{
public function testing()
{
return "I am called.";
}
public function __call($name, $args)
{
if($name == 'testingOther')
{
return call_user_func_array(array($this, 'testing'), $args);
}
}
}
$t = new TestCallable();
echo $t->testing(); // Output: I am called.
echo $t->testingOther(); // Output: I am called.
echo $t->working(); // Output: (null)
echo is_callable(array($t, 'testing')); // Output: TRUE
echo is_callable(array($t, 'testingOther')); // Output: TRUE
echo is_callable(array($t, 'working')); // Output: TRUE, expected: FALSE
?>
Note that -- as mentioned in the migration guide-- starting from PHP 8.0, is_callable() will not work with non-static methods if you use a class name, instead an object of the class should be provided:
<?php
class Test
{
public function method1() { }
public static function method2() { }
}
// Pre PHP 8
var_dump(is_callable(array('Test', 'method1'))); // bool(true)
var_dump(is_callable(array('Test', 'method2'))); // bool(true)
// Post PHP 8
var_dump(is_callable(array('Test', 'method1'))); // bool(false)
var_dump(is_callable(array('Test', 'method2'))); // bool(true)
var_dump(is_callable(array(new Test, 'method1'))); // bool(true)
?>