is_callable
(PHP 4 >= 4.0.6, PHP 5, PHP 7, PHP 8)
is_callable — 验证值是否可以在当前范围内作为函数调用
说明
验证 value
是 callable,或者其可以使用
call_user_func() 函数调用。
参数
示例
示例 #1 检查字符串是否可以作为函数调用
<?php
function someFunction() {}
$functionVariable = 'someFunction';
var_dump(is_callable($functionVariable, false, $callable_name));
var_dump($callable_name);
?>
以上示例会输出:
bool(true) string(12) "someFunction"
示例 #2 检查数组是否可以作为函数调用
<?php
class SomeClass
{
public function someMethod() {}
}
$anObject = new SomeClass();
$methodVariable = [$anObject, 'someMethod'];
var_dump(is_callable($methodVariable, true, $callable_name));
var_dump($callable_name);
?>
以上示例会输出:
bool(true) string(21) "SomeClass::someMethod"
示例 #3 is_callable() 和构造方法
尽管构造方法是创建对象时调用的方法,但它们不是静态方法,is_callable()
将对它们返回 false
。无法使用 is_callable() 检查类是否可以从当前作用域实例化。
<?php
class Foo
{
public function __construct() {}
public function foo() {}
}
var_dump(
is_callable(['Foo', '__construct']),
is_callable(['Foo', 'foo'])
);
$foo = new Foo();
var_dump(is_callable([$foo, '__construct']));
?>
以上示例会输出:
bool(false) bool(false) bool(true)
注释
- 如果对象实现了 __invoke() 且该方法在当前作用域可见,则始终可调用。
- 如果类名实现了 __callStatic(),则可调用。
-
如果对象实现 __call(),则此函数对该对象的任何方法将返回
true
,即使该方法没有定义。 - 如果使用类名调用此函数,则会触发自动加载。
参见
- call_user_func() - 把第一个参数作为回调函数调用
- function_exists() - 如果给定的函数已经被定义就返回 true
- method_exists() - 检查类的方法是否存在
+添加备注
用户贡献的备注 2 notes
izharaazmi at gmail dot com ¶
9 years ago
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
?>
mohamed dot elidrissi at protonmail dot com ¶
3 years ago
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)
?>