首页 > 开发 > PHP > 正文

php method_exists 检测类中是否包括函数

2024-05-04 21:59:21
字体:大 中 小
来源:转载
供稿:网友

php method_exists 检测类中是否包括函数.

method_exists() 函数的语法如下:bool method_exists(object object,string method_name).

method_exists() 函数的作用是检查类的方法是否存在,如果 method_name 所指的方法在 object 所指的对象类中已定义,则返回 true,否则返回 false,实例代码如下:

  1. class a { 
  2.     public function xx(){ 
  3.         echo 'xx'; 
  4.     } 
  5.      
  6.     public function yy() { 
  7.         echo 'yy'; 
  8.     } 
  9. } 
  10. $obj = new a(); 
  11. var_dump(method_exists($obj, 'xx')); 
  12. var_dump(method_exists($obj, 'xx')); 
  13. var_dump(method_exists($obj, 'xx')); 
  14. //测试结果都为true 
  15. //开源软件:Vevb.com 
  16. class a { 
  17.     public function xx(){ 
  18.         echo 'xx'; 
  19.     } 
  20.      
  21.     public function yy() { 
  22.         echo 'yy'; 
  23.     } 
  24.     public function yy() { 
  25.         echo 'yy'; 
  26.     } 
  27. } 
  28. $obj = new a(); 
  29. $obj->yy(); 
  30. $obj->yy(); 

以上语句报错,今天才发现原来php的对象属性是不区分大小写的.

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表