加入收藏 | 设为首页 | 会员中心 | 我要投稿 云计算网_泰州站长网 (http://www.0523zz.com/)- 视觉智能、AI应用、CDN、行业物联网、智能数字人!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

深入认识PHP反射API!

发布时间:2022-07-21 13:58:39 所属栏目:PHP教程 来源:互联网
导读:PHP中的反射API就像Java中的java.lang.reflect包一样。它由一系列可以分析属性、方法和类的内置类组成。它在某些方面和对象函数相似,比如get_class_vars(),但是更加灵活,而且可以提供更多信息。 反射API的部分类 使用反射API这些类,可以获得在运行时访问
  PHP中的反射API就像Java中的java.lang.reflect包一样。它由一系列可以分析属性、方法和类的内置类组成。它在某些方面和对象函数相似,比如get_class_vars(),但是更加灵活,而且可以提供更多信息。
 
  反射API的部分类
 
  使用反射API这些类,可以获得在运行时访问对象、函数和脚本中的扩展的信息。通过这些信息可以用来分析类或者构建框架。
 
  类 描 述
  Reflection 为类的摘要信息提供静态函数export()
  ReflectionClass 类信息和工具
  ReflectionMethod 类方法信息和工具
  ReflectionParameter 方法参数信息
  ReflectionProperty 类属性信息
  ReflectionFunction 函数信息和工具
  ReflectionExtension PHP扩展信息
  ReflectionException 错误类
  获取类的信息
 
  我们在工作中使用过一些用于检查类属性的函数,例如:get_class_methods、getProduct等。这些方法对获取详细类信息有很大的局限性。
 
  我们可以通过反射API类:Reflection 和 ReflectionClass 提供的静态方法 export 来获取类的相关信息, export 可以提供类的几乎所有的信息,包括属性和方法的访问控制状态、每个方法需要的参数以及每个方法在脚本文档中的位置。这两个工具类, export 静态方法输出结果是一致的,只是使用方式不同。
 
  首先,构建一个简单的类
 
 
  class Student {
 
      public    $name;
 
      protected $age;
 
      private   $sex;
 
   
 
      public function __construct($name, $age, $sex)
 
      {
 
          $this->setName($name);
 
          $this->setAge($age);
 
          $this->setSex($sex);
 
 
      protected function setAge($age)
 
      {
 
          $this->age = $age;
 
      }
 
   
 
      private function setSex($sex)
 
      {
 
          $this->sex = $sex;
 
      }
 
  }
 
  使用 ReflectionClass::export() 获取类信息
 
 
  ReflectionClass::export('Student');
 
  打印结果:
 
 
  Class [ class Student ] {
 
      @@ D:wampwwwtest2.php 3-29
 
      - Constants [0] { }
 
      - Static properties [0] { }
 
      - Static methods [0] { }
 
      - Properties [3] {
 
          Property [ public $name ]
 
          Property [ protected $age ]
 
          Property [ private $sex ]
 
      }
 
      - Methods [4] {
 
          Method [ public method __construct ] {
 
              @@ D:wampwwwtest2.php 8 - 13
 
              - Parameters [3] {
 
                  Parameter #0 [ $name ]
 
                  Parameter #1 [ $age ]
 
                  Parameter #2 [ $sex ]
 
              - Parameters [1] {
 
                  Parameter #0 [ $age ]
 
              }
 
          }
 
          Method [ private method setSex ] {
 
              @@ D:wampwwwtest2.php 25 - 28
 
              - Parameters [1] {
 
                  Parameter #0 [ $sex ]
 
              }
 
          }
 
      }
 
  }
 
  ReflectionClass类提供了非常多的工具方法,官方手册给的列表如下:
 
 
  ReflectionClass::__construct — 初始化 ReflectionClass 类
 
  ReflectionClass::export — 导出一个类
 
  ReflectionClass::getConstant — 获取定义过的一个常量
 
  ReflectionClass::getConstants — 获取一组常量
 
  ReflectionClass::getConstructor — 获取类的构造函数
 
  ReflectionClass::getDefaultProperties — 获取默认属性
 
  ReflectionClass::getDocComment — 获取文档注释
 
  ReflectionClass::getEndLine — 获取最后一行的行数
 
  ReflectionClass::getName — 获取类名
 
  ReflectionClass::getNamespaceName — 获取命名空间的名称
 
  ReflectionClass::getParentClass — 获取父类
 
  ReflectionClass::getProperties — 获取一组属性
 
  ReflectionClass::getProperty — 获取类的一个属性的 ReflectionProperty
 
  ReflectionClass::getReflectionConstant — Gets a ReflectionClassConstant for a class's constant
 
  ReflectionClass::getReflectionConstants — Gets class constants
 
  ReflectionClass::getShortName — 获取短名
 
  ReflectionClass::getStartLine — 获取起始行号
 
  ReflectionClass::getStaticProperties — 获取静态(static)属性
 
  ReflectionClass::getStaticPropertyValue — 获取静态(static)属性的值
 
  ReflectionClass::getTraitAliases — 返回 trait 别名的一个数组
 
  ReflectionClass::getTraitNames — 返回这个类所使用 traits 的名称的数组
 
  ReflectionClass::getTraits — 返回这个类所使用的 traits 数组
 
  ReflectionClass::hasConstant — 检查常量是否已经定义
 
  ReflectionClass::hasMethod — 检查方法是否已定义
 
  ReflectionClass::hasProperty — 检查属性是否已定义
 
  ReflectionClass::implementsInterface — 接口的实现
 
  ReflectionClass::inNamespace — 检查是否位于命名空间中
 
  ReflectionClass::isAbstract — 检查类是否是抽象类(abstract)
 
  ReflectionClass::isAnonymous — 检查类是否是匿名类
 
  ReflectionClass::isCloneable — 返回了一个类是否可复制
 
  ReflectionClass::isFinal — 检查类是否声明为 final
 
  ReflectionClass::isTrait — 返回了是否为一个 trait
 
  ReflectionClass::isUserDefined — 检查是否由用户定义的
 
  ReflectionClass::newInstance — 从指定的参数创建一个新的类实例
 
  ReflectionClass::newInstanceArgs — 从给出的参数创建一个新的类实例。
 
  ReflectionClass::newInstanceWithoutConstructor — 创建一个新的类实例而不调用它的构造函数
 
  ReflectionClass::setStaticPropertyValue — 设置静态属性的值
 
  ReflectionClass::__toString — 返回 ReflectionClass 对象字符串的表示形式。
 
  使用 Reflection::export() 获取类信息
 
 
 
  $prodClass = new ReflectionClass('Student');
 
  Reflection::export($prodClass);

(编辑:云计算网_泰州站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读