站长资讯网
最全最丰富的资讯网站

php怎么判断类中是否定义了指定方法

php中可以使用method_exists()函数来判断类中是否定义了指定方法,该函数可检查类的指定方法是否存在,语法“method_exists($object,$method_name)”;如果定义了则返回true,否则返回false。

php怎么判断类中是否定义了指定方法

本教程操作环境:windows7系统、PHP7.1版,DELL G3电脑

在php中,可以使用method_exists()函数来判断类中是否定义了指定方法。

示例:

$directory=new Directory; if(!method_exists($directory,'read')){ echo '未定义read方法!'; }

说明:

method_exists()函数可以检查类的指定方法是否存在。语法:

method_exists(mixed $object, string $method_name): bool

该函数会检查类的方法是否存在于指定的 object中。

  • object:对象示例或者类名。

  • method_name:方法名。

推荐学习:《PHP视频教程》

赞(0)
分享到: 更多 (0)