CakeFest 2024: The Official CakePHP Conference

is_callable

(PHP 4 >= 4.0.6, PHP 5, PHP 7, PHP 8)

is_callable 验证值是否可以在当前范围内作为函数调用。

说明

is_callable(mixed $value, bool $syntax_only = false, string &$callable_name = null): bool

验证值是 callable

参数

value

要验证的值

syntax_only

如果设置为 true,则函数仅验证 value 可能是函数或方法。它仅拒绝不是字符串的简单变量,或者不能用作回调的有效结构的数组。只有 2 个条目有效,一个是对象或者字符串,其次是字符串。

callable_name

接受“可调用的名称”。下面的例子是“someClass::someMethod”。注意,尽管 someClass::SomeMethod() 暗示是可调用的静态方法,但事实并非如此。

返回值

如果 value 可调用则返回 true,否则返回 false

示例

示例 #1 is_callable() 例子

<?php
// 如何检测变量以查看是否可以作为函数调用。

//
// 包含函数的简单变量
//

function someFunction()
{
}

$functionVariable = 'someFunction';

var_dump(is_callable($functionVariable, false, $callable_name)); // bool(true)

echo $callable_name, "\n"; // someFunction

//
// 包含方法的数组
//

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

?>

示例 #2 is_callable() 和构造函数

is_callable() 报告构造函数不可调用。

<?php

class Foo
{
public function
__construct() {}
public function
foo() {}
}

var_dump(
is_callable(array('Foo', '__construct')),
is_callable(array('Foo', 'foo'))
);

以上示例会输出:

bool(false)
bool(false)

注释

  • 如果对象实现了 __invoke() 且该方法在当前作用域可见,则始终可调用。
  • 如果类名实现了 __callStatic(),则可调用。
  • 如果对象实现 __call(),则此函数对该对象的任何方法将返回 true,即使该方法没有定义。
  • 如果使用类名调用此函数,则会触发自动加载。

参见

add a note

User Contributed Notes 2 notes

up
34
izharaazmi at gmail dot com
8 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
?>
up
17
mohamed dot elidrissi at protonmail dot com
2 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)

?>
To Top