php 中 function_exists 、 method_exists 和 is_callable
2012-06-04 18:19
1346 查看
function_exists — Return
the given function has been defined
method_exists — Checks
if the class method exists
is_callable — Verify
that the contents of a variable can be called as a function
function_exists 比较简单点就是判断函数有没有被定义 而method_exists 是判断类内的方法存不存在 is_callable 检测参数是否为合法的可调用结构
返回值 都是 bool
但是参数不同
function_exists 只有一个参数 函数名 $string
method_exists 两个参数 $object 类对象 $string 方法名
is_callable 三个参数 $var mixed 可以是string array $syntax_only bool $callback_name string
如果is_callable的第一个参数 是 string 那么 和 function_exists 相似 如果是数组 则和 method_exists
但又有不同
method_exists不会考虑类方法的定义范围 public protected private
而 is_callable 会在方法是被 protected private 返回 false
is_callable判断是会去调用__call魔术方法来判断,而method_exists不会
用php.net上的例子解释就是:
Example:
<?php
class Test {
public function testing($not = false)
{
$not = $not ? 'true' : 'false';
echo "testing - not: $not<br/>";
}
public function __call($name, $args)
{
if(preg_match('/^not([A-Z]\w+)$/', $name, $matches))
{
$fn_name = strtolower($matches[1]);
if(method_exists($this, $fn_name))
{
$args[]
= true; //
add NOT boolean to args
return call_user_func_array(array($this, $matches[1]), $args);
}
}
die("No method with name: $name<br/>");
}
}
$t =
new Test();
$t->testing();
$t->notTesting();
echo "exists: ".method_exists($t, 'notTesting').'<br/>';
echo "callable: ".is_callable(array($t, 'notTesting'));
?>
Output:
testing - not: false
testing - not: true
exists:
callable: 1
TRUEif
the given function has been defined
method_exists — Checks
if the class method exists
is_callable — Verify
that the contents of a variable can be called as a function
function_exists 比较简单点就是判断函数有没有被定义 而method_exists 是判断类内的方法存不存在 is_callable 检测参数是否为合法的可调用结构
返回值 都是 bool
但是参数不同
function_exists 只有一个参数 函数名 $string
method_exists 两个参数 $object 类对象 $string 方法名
is_callable 三个参数 $var mixed 可以是string array $syntax_only bool $callback_name string
如果is_callable的第一个参数 是 string 那么 和 function_exists 相似 如果是数组 则和 method_exists
但又有不同
method_exists不会考虑类方法的定义范围 public protected private
而 is_callable 会在方法是被 protected private 返回 false
is_callable判断是会去调用__call魔术方法来判断,而method_exists不会
用php.net上的例子解释就是:
Example:
<?php
class Test {
public function testing($not = false)
{
$not = $not ? 'true' : 'false';
echo "testing - not: $not<br/>";
}
public function __call($name, $args)
{
if(preg_match('/^not([A-Z]\w+)$/', $name, $matches))
{
$fn_name = strtolower($matches[1]);
if(method_exists($this, $fn_name))
{
$args[]
= true; //
add NOT boolean to args
return call_user_func_array(array($this, $matches[1]), $args);
}
}
die("No method with name: $name<br/>");
}
}
$t =
new Test();
$t->testing();
$t->notTesting();
echo "exists: ".method_exists($t, 'notTesting').'<br/>';
echo "callable: ".is_callable(array($t, 'notTesting'));
?>
Output:
testing - not: false
testing - not: true
exists:
callable: 1
相关文章推荐
- PHP的method_exists,function_exists,is_callable的区别
- 【PHP】function_exists , method_exists 与 is_callable的区别
- function_exists 、 method_exists 和 is_callable 的区别
- function_exists, method_exists, is_callable的区别
- 【PHP】is_callable()与method_exists()函数
- php 函数method_exists()与is_callable()的区别
- 判断某个方法是否存在,解析php函数function_exists (),method_exists()与is_callable()的区别
- PHP中file_exists与is_file,is_dir的区别介绍
- php中“Deprecated: Function split() is deprecated in”错误解决办法
- PHP Warning: date() [function.date]: It is not safe to rely on the system's timezone
- php函数method_exists()与is_callable()的区别
- PHP中file_exists与is_file,is_dir的区别介绍
- 解析php函数method_exists()与is_callable()的区别
- PHP Warning: date() [function.date]: It is not safe to rely on the system's timezone
- PHP中file_exists与is_file、is_dir的区别,以及执行效率的比较
- PHP中魔术变量__METHOD__与__FUNCTION__的区别
- PHP Warning: date() [function.date]: It is not saf
- 解析php函数method_exists()与is_callable()的区别
- PHP中file_exists与is_file、is_dir的区别,以及执行效率的比较
- php通过function_exists检测函数是否存在的方法