热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

phpinterface_exists、class_exists、method_exists和property_exists介绍

下面我们一起来看在php中PHP类和对象函数这phpinterface_exists、class_exists、method_exists和property_exists详解,希望文章对各

下面我们一起来看在php 中PHP类和对象函数这 php interface_exists、class_exists、method_exists和property_exists详解,希望文章对各位同学会有所帮助。

1. interface_exists、class_exists、method_exists和property_exists:

顾名思义,从以上几个函数的命名便可以猜出几分他们的功能。我想这也是我随着对PHP的深入学习而越来越喜欢这门编程语言的原因了吧。下面先给出他们的原型声明和简短说明,更多的还是直接看例子代码吧。

bool interface_exists (string $interface_name [, bool $autoload = true ]) 判断接口是否存在,第二个参数表示在查找时是否执行__autoload。

bool class_exists (string $class_name [, bool $autoload = true ]) 判断类是否存在,第二个参数表示在查找时是否执行__autoload。

bool method_exists (mixed $object , string $method_name) 判断指定类或者对象中是否含有指定的成员函数。

bool property_exists (mixed $class , string $property) 判断指定类或者对象中是否含有指定的成员变量。

实例代码如下:

  1. //in another_test_class.php 
  2. interface AnotherTestInterface { 
  3. class AnotherTestClass { 
  4.     public static function printMe() { 
  5.         print "This is Test2::printSelf.n"
  6.     } 
  7.     public function doSomething() { 
  8.         print "This is Test2::doSomething.n"
  9.     } 
  10.     public function doSomethingWithArgs($arg1$arg2) { 
  11.         print 'This is Test2::doSomethingWithArgs with ($arg1 = '.$arg1.' and $arg2 = '.$arg2.").n"
  12.     } 
  13. //in class_exist_test.php, 下面测试代码中所需的类和接口位于another_test_class.php, 
  14. //由此可以发现规律,类和接口的名称是驼峰风格的,而文件名的单词间是下划线分隔的。 
  15. //这里给出了两种__autoload的方式,因为第一种更为常用和方便,因此我们这里将第二种方式注释掉了,他们之间的差别可以查看manual。 
  16. function __autoload($classname) { 
  17.     $nomilizedClassname = strtolower(preg_replace('/([A-Z]w*)([A-Z]w*)([A-Z]w*)/','${1}_${2}_${3}',$classname)); 
  18.     require strtolower($nomilizedClassname).".php"
  19. //spl_autoload_register(function($classname) { 
  20. //    $nomilizedClassname = strtolower(preg_replace('/([A-Z]w*)([A-Z]w*)([A-Z]w*)/','${1}_${2}_${3}',$classname)); 
  21. //    require strtolower($nomilizedClassname).".php"; 
  22. //}); 
  23. print "The following case is tested before executing autoload.n"
  24. if (!class_exists('AnotherTestClass',false)) { 
  25.     print "This class doesn't exist if no autoload.n"
  26. if (!interface_exists('AnotherTestInterface',false)) { 
  27.     print "This interface doesn't exist if no autoload.n"
  28. print "nThe following case is tested after executing autoload.n"
  29. if (class_exists('AnotherTestClass',true)) { 
  30.     print "This class exists if autoload is set to true.n"
  31. if (interface_exists('AnotherTestInterface',true)) { 
  32.     print "This interface exists if autoload is set to true.n"
  33. }     

运行结果如下:

  1. bogon:TestPhp$ php class_exist_test.php  
  2. The following case is tested before executing autoload. 
  3. This class doesn't exist if no autoload. 
  4. This interface doesn't exist if no autoload. 
  5. The following case is tested after executing autoload. 
  6. This class exists if autoload is set to true. 
  7. This interface exists if autoload is set to true.2. get_declared_classes和get_declared_interfaces: 

分别返回当前可以访问的所有类和接口,这不仅包括自定义类和接口,也包括了PHP内置类和接口。他们的函数声明非常简单,没有参数,只是返回数组。见如下实例代码:

  1. interface AnotherTestInterface { 
  2. class AnotherTestClass { 
  3.     public static function printMe() { 
  4.         print "This is Test2::printSelf.n"
  5.     } 
  6. print_r(get_declared_interfaces()); 
  7. print_r(get_declared_classes());  

由于输出结果过长,而且这两个函数也比较简单,所以下面就不再给出输出结果了。

3. get_class_methods、get_class_vars和get_object_vars:

这三个函数有一个共同点,即只能获取作用域可见范围内的所有成员函数、成员变量或非静态成员变量。比如在类的内部调用,则所有成员函数或者变量都符合条件,而在类的外部,则只有共有的函数和变量可以返回。

array get_class_methods (mixed $class_name) 获取指定类中可访问的成员函数。

array get_class_vars (string $class_name) 获取指定类中可以访问的成员变量。

array get_object_vars (object $object) 获取可以访问的非静态成员变量。

代码如下:

  1. function output_array($functionName$items) { 
  2.     print "$functionName.....................n"
  3.     foreach ($items as $key => $value) { 
  4.         print '$key = '.$key' => $value = '.$value."n"
  5.     } 
  6. class TestClass { 
  7.     public $publicVar = 1; 
  8.     private $privateVar = 2; 
  9.     static private $staticPrivateVar = "hello"
  10.     static public $staticPublicVar
  11.     private function privateFunction() { 
  12.     } 
  13.     function publicFunction() { 
  14.         output_array("get_class_methods",get_class_methods(__CLASS__)); 
  15.         output_array('get_class_vars',get_class_vars(__CLASS__)); 
  16.         output_array('get_object_vars',get_object_vars($this)); 
  17.     } 
  18. $testObj = new TestClass(); 
  19. print "The following is output within TestClass.n"
  20. $testObj->publicFunction(); 
  21. print "nThe following is output out of TestClass.n"
  22. output_array('get_class_methods',get_class_methods('TestClass')); 
  23. output_array('get_class_vars',get_class_vars('TestClass')); 
  24. output_array('get_object_vars',get_object_vars($testObj));    运行结果如下: 
  25.  
  26. bogon:TestPhp liulei$ php class_exist_test.php  
  27. The following is output within TestClass. 
  28. get_class_methods..................... 
  29. $key = 0 => $value = privateFunction 
  30. $key = 1 => $value = publicFunction 
  31. get_class_vars..................... 
  32. $key = publicVar => $value = 1 
  33. $key = privateVar => $value = 2 
  34. $key = staticPrivateVar => $value = hello 
  35. $key = staticPublicVar => $value =  
  36. get_object_vars..................... 
  37. $key = publicVar => $value = 1 
  38. $key = privateVar => $value = 2 
  39. The following is output out of TestClass. 
  40. get_class_methods..................... 
  41. $key = 0 => $value = publicFunction 
  42. get_class_vars..................... 
  43. $key = publicVar => $value = 1 
  44. $key = staticPublicVar => $value =  
  45. get_object_vars..................... 
  46. $key = publicVar => $value = 14. get_called_class和get_class: 
  47. string get_class ([ object $object = NULL ])  www.111Cn.net获取参数对象的类名称。 
  48. string get_called_class (void) 静态方法调用时当前的类名称。 
  49.  
  50. class Base { 
  51.     static public function test() { 
  52.         var_dump(get_called_class()); 
  53.     } 
  54. class Derive extends Base { 
  55. Base::test(); 
  56. Derive::test(); 
  57. var_dump(get_class(new Base())); 
  58. var_dump(get_class(new Derive()));    
  59. 运行结果如下: 
  60. bogon:TestPhp$ php another_test_class.php  
  61. string(4) "Base" 
  62. string(6) "Derive" 
  63. string(4) "Base" 
  64. string(6) "Derive" 

5. get_parent_class、is_a和is_subclass_of:

这三个函数都是和类的继承相关,所以我把他们归到了一起。

string get_parent_class ([ mixed $object ]) 获取参数对象的父类,如果没有父类则返回false。

bool is_a (object $object, string $class_name) 判断第一个参数对象是否是$class_name类本身或是其父类的对象。

bool is_subclass_of (mixed $object, string $class_name) 判断第一个参数对象是否是$class_name的子类。

代码如下:

  1. class Base { 
  2.     static public function test() { 
  3.         var_dump(get_called_class()); 
  4.     } 
  5. class Derive extends Base { 
  6. var_dump(get_parent_class(new Derive())); 
  7. var_dump(is_a(new Derive(),'Derive')); 
  8. var_dump(is_a(new Derive(),'Base')); 
  9. var_dump(is_a(new Base(),'Derive')); 
  10. var_dump(is_subclass_of(new Derive(),'Derive')); 
  11. var_dump(is_subclass_of(new Derive(),'Base'));    运行结果如下: 
  12.  
  13. bogon:TestPhp$ php another_test_class.php  
  14. string(4) "Base" 
  15. bool(true) 
  16. bool(true) 
  17. bool(false) 
  18. bool(false) 
  19. bool(true) 

推荐阅读
  • LeetCode 540:有序数组中的唯一元素
    来源:力扣(LeetCode),链接:https://leetcode-cn.com/problems/single-element-in-a-sorted-array。题目要求在仅包含整数的有序数组中,找到唯一出现一次的元素,并确保算法的时间复杂度为 O(log n) 和空间复杂度为 O(1)。 ... [详细]
  • 如何在Faceu激萌中设置和使用妆容切换特效?
    本文将详细介绍如何在Faceu激萌应用中设置和使用妆容切换特效,帮助用户轻松实现创意拍摄。无论是新手还是有经验的用户,都能从中受益。 ... [详细]
  • 本文介绍如何解决在 IIS 环境下 PHP 页面无法找到的问题。主要步骤包括配置 Internet 信息服务管理器中的 ISAPI 扩展和 Active Server Pages 设置,确保 PHP 脚本能够正常运行。 ... [详细]
  • Python 异步编程:深入理解 asyncio 库(上)
    本文介绍了 Python 3.4 版本引入的标准库 asyncio,该库为异步 IO 提供了强大的支持。我们将探讨为什么需要 asyncio,以及它如何简化并发编程的复杂性,并详细介绍其核心概念和使用方法。 ... [详细]
  • 探讨一个老旧 PHP MySQL 系统中,时间戳字段不定期出现异常值的问题及其可能原因。 ... [详细]
  • 国内BI工具迎战国际巨头Tableau,稳步崛起
    尽管商业智能(BI)工具在中国的普及程度尚不及国际市场,但近年来,随着本土企业的持续创新和市场推广,国内主流BI工具正逐渐崭露头角。面对国际品牌如Tableau的强大竞争,国内BI工具通过不断优化产品和技术,赢得了越来越多用户的认可。 ... [详细]
  • 优化ListView性能
    本文深入探讨了如何通过多种技术手段优化ListView的性能,包括视图复用、ViewHolder模式、分批加载数据、图片优化及内存管理等。这些方法能够显著提升应用的响应速度和用户体验。 ... [详细]
  • 郑州大学在211高校中的地位与排名解析
    本文将详细解读郑州大学作为一所位于河南省的211和双一流B类高校,在全国211高校中的地位与排名,帮助高三学生更好地了解这所知名学府的实力与发展前景。 ... [详细]
  • 深入理解 Oracle 存储函数:计算员工年收入
    本文介绍如何使用 Oracle 存储函数查询特定员工的年收入。我们将详细解释存储函数的创建过程,并提供完整的代码示例。 ... [详细]
  • 优化ASM字节码操作:简化类转换与移除冗余指令
    本文探讨如何利用ASM框架进行字节码操作,以优化现有类的转换过程,简化复杂的转换逻辑,并移除不必要的加0操作。通过这些技术手段,可以显著提升代码性能和可维护性。 ... [详细]
  • 本文总结了2018年的关键成就,包括职业变动、购车、考取驾照等重要事件,并分享了读书、工作、家庭和朋友方面的感悟。同时,展望2019年,制定了健康、软实力提升和技术学习的具体目标。 ... [详细]
  • 电子元件封装库:三极管、MOS管及部分LDO(含3D模型)
    本资源汇集了常用的插件和贴片三极管、MOS管以及部分LDO的封装,涵盖TO和SOT系列。所有封装均配有高质量的3D模型,共计96种,满足日常设计需求。 ... [详细]
  • 在计算机技术的学习道路上,51CTO学院以其专业性和专注度给我留下了深刻印象。从2012年接触计算机到2014年开始系统学习网络技术和安全领域,51CTO学院始终是我信赖的学习平台。 ... [详细]
  • CSS 布局:液态三栏混合宽度布局
    本文介绍了如何使用 CSS 实现液态的三栏布局,其中各栏具有不同的宽度设置。通过调整容器和内容区域的属性,可以实现灵活且响应式的网页设计。 ... [详细]
  • 本文详细介绍了如何使用PHP检测AJAX请求,通过分析预定义服务器变量来判断请求是否来自XMLHttpRequest。此方法简单实用,适用于各种Web开发场景。 ... [详细]
author-avatar
由来只有新人笑_谁能记得旧人哭
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有