array ( 0 => 'index.php', 1 => 'PHP Manual', ), 'head' => array ( 0 => 'UTF-8', 1 => 'zh', ), 'this' => array ( 0 => 'function.is-callable.php', 1 => 'is_callable', ), 'up' => array ( 0 => 'ref.var.php', 1 => '变量处理函数', ), 'prev' => array ( 0 => 'function.is-bool.php', 1 => 'is_bool', ), 'next' => array ( 0 => 'function.is-countable.php', 1 => 'is_countable', ), 'alternatives' => array ( ), 'source' => array ( 'lang' => 'zh', 'path' => 'reference/var/functions/is-callable.xml', ), ); $setup["toc"] = $TOC; $setup["toc_deprecated"] = $TOC_DEPRECATED; $setup["parents"] = $PARENTS; manual_setup($setup); ?>
(PHP 4 >= 4.0.6, PHP 5, PHP 7, PHP 8)
is_callable — 验证值是否可以在当前范围内作为函数调用。
验证值是 callable。
value
要验证的值
syntax_only
如果设置为 true
,则函数仅验证 value
可能是函数或方法。它仅拒绝不是字符串的简单变量,或者不能用作回调的有效结构的数组。只有
2 个条目有效,一个是对象或者字符串,其次是字符串。
callable_name
接受“可调用的名称”。下面的例子是“someClass::someMethod”。注意,尽管 someClass::SomeMethod() 暗示是可调用的静态方法,但事实并非如此。
示例 #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)
true
,即使该方法没有定义。