php判断类方法是否存在的方法
墨初 编程开发 1558阅读
想在判断php类中的某个指定的方法是否存在,可以使用php中内置的函数method_exists(),如果指定的方法存在则返回TRUE否则就返回FALSE,下面73so博客就详细的介绍一下这个method_exists()这个函数以及这个函数的使用方法。
php method_exists() 函数
method_exists():此函数检查类的方法是否存在于某个指定的object中,如果存在则返回TRUE,如果不存在则返回FALSE.
语法:
method_exists(object|string $object_or_class, string $method): bool
参数:
| 参数 | 含义 |
|---|---|
| $object_or_class | 对象示例或者类名 |
| $method | 方法名 |
php method_exists() 使用方法
例1:
$directory = new Directory;
if(!method_exists($directory,'read')){
echo '未定义read方法!';
}
例2:
class soso {
function e()
{
}
}
# 73so.com
$so = new soso();
var_dump(method_exists($so,'e'));
// bool(true)
var_dump(method_exists($so,'b'));
// bool(false)
以上就是关于php中利用method_exists()函数来判断类中的指定方法是否存在的例子,大家可以借鉴一下。