我正在开发一个项目,在该项目中我使用了旧版本中已弃用的函数。但不希望我的脚本在旧版本中使用时停止。
所以我检查该函数是否存在,如果不存在则创建它。
有什么区别function_exists
and is_callable
PHP 中哪个更好用?
if (!is_callable('xyz')) {
function xyz() {
// code goes here
}
}
OR
if(!function_exists('xyz')) {
function xyz() {
// code goes here
}
}
功能is_callable http://php.net/is_callable不仅接受函数名称,还接受其他类型的回调 http://php.net/callback:
Foo::method
array("Foo", "method")
array($obj, "method")
- 闭包和其他可调用对象 (PHP 5.3)
So is_callable
接受任何你可以通过的东西call_user_func http://php.net/call_user_func和家人,同时function_exists http://php.net/function_exists只告诉某个函数是否存在(不是方法,请参阅method_exists http://php.net/method_exists为此,也不关闭)。
换一种方式,is_callable
是一个包装器zend_is_callable http://lxr.php.net/opengrok/xref/PHP_TRUNK/Zend/zend_API.c#3035,它使用伪类型回调处理变量,而function_exists
只做一个哈希表查找 http://lxr.php.net/opengrok/xref/PHP_TRUNK/Zend/zend_builtin_functions.c#1295在函数表中。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)