您的位置:首页 > 技术中心 > PHP教程 >

php怎么查询类中方法是否存在

时间:2023-01-06 20:33

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

本教程操作环境:windows7系统、PHP8版、DELL G3电脑

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

php method_exists()函数

PHP method_exists函数——检查方法是否存在于类中,method_exists函数可检查类的方法是否在对象中存在。如果存在,则返回true,否则返回false。

语法:

bool method_exists ( object object, string method_name )
  • object为必选参数,输入的对象名称;

  • method_name为必选参数,输入的类方法名称。

method_exists()函数的使用示例

本示例主要应用method_exists()函数检查类的方法是否属于$book对象的类。代码如下:

<?php
header("Content-type:text/html;charset=utf-8");
class Book{
   function php(){
   }
   function asp(){
   }
   function jsp(){
   }
}
$book = new Book();
if(method_exists($book, 'php')){
   echo 'php 方法存在于$book 对象的类中<br>';
}
else{
   echo 'php 方法不存在于$book 对象的类中<br>';
}
if(method_exists($book, 'java')){
   echo 'java 方法存在于$book 对象的类中<br>';
}
else{
   echo 'java 方法不存在于$book 对象的类中<br>';
}
?>

本示例的运行结果如下:

1.png

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

以上就是php怎么查询类中方法是否存在的详细内容,更多请关注gxlsystem.com其它相关文章!

本类排行

今日推荐

热门手游