• PHP反射API的使用、体会、说明


    最近开发支付宝相关功能的时候,由于支付宝的SDK比较落伍,不支持composer的方式加载,使用三方的composer SDK又觉得不放心

    为了简化代码的调用方式,使用PHP的反射类针对支付宝官方SDK做了一层封装,使开发中仅需要关心业务层即可,理论上实现了支付宝SDK全功能反射服务

    有需要的的同学可以安装体验一下:

    composer require jiujiude/alipay-sdk

    下面整理了一些反射类相关的知识

    反射是什么?

    它是指在PHP运行状态中,扩展分析PHP程序,导出或提取出关于类、方法、属性、参数等的详细信息,包括注释。

    这种动态获取的信息以及动态调用对象的方法的功能称为反射API。

    反射是操纵面向对象范型中元模型的API,其功能十分强大,可帮助我们构建复杂,可扩展的应用。

    可以干什么?

    可以做自动加载插件,自动生成文档,甚至可用来扩充PHP语言,可谓十分强大。

    反射api由若干类组成,可帮助我们用来访问程序的元数据或者同相关的注释交互。
    借助反射我们可以获取诸如类实现了那些方法,创建一个类的实例(不同于用new创建),调用一个方法(也不同于常规调用),传递参数,动态调用类的静态方法。
    反射api是PHP内建的OOP技术扩展,包括一些类,异常和接口,综合使用他们可用来帮助我们分析其它类,接口,方法,属性,方法和扩展。这些OOP扩展被称为反射。
     
    目前很多主流的框架底层都在使用,比如Laravel的服务容器,ThinkPHP5的容器和依赖注入等等
    可以替代get_class_methods();get_class_vars();call_user_func();call_user_func_array();.....诸如此类的方法
     
    如何使用?
    平常我们用的比较多的是 ReflectionClass类 和 ReflectionMethod类,例如:
    <?php
    class Person {
     
     /**
      * For the sake of demonstration, we"re setting this private
      */
     private $_allowDynamicAttributes = false;
    
     /**
      * type=primary_autoincrement
      */
     protected $id = 0;
    
     /**
      * type=varchar length=255 null
      */
     protected $name;
    
     /**
      * type=text null
      */
     protected $biography;
    
     public function getId() {
      return $this->id;
     }
    
     public function setId($v) {
      $this->id = $v;
     }
    
     public function getName() {
      return $this->name;
     }
    
     public function setName($v) {
      $this->name = $v;
     }
    
     public function getBiography() {
      return $this->biography;
     }
    
     public function setBiography($v) {
      $this->biography = $v;
     }
    }

    一、通过ReflectionClass,我们可以得到Person类的以下信息:

    • 1.常量 Contants
    • 2.属性 Property Names
    • 3.方法 Method Names静态
    • 4.属性 Static Properties
    • 5.命名空间 Namespace
    • 6.Person类是否为final或者abstract
    • 7.Person类是否有某个方法

    接下来反射它,只要把类名"Person"传递给ReflectionClass就可以了

    $class = new ReflectionClass('Person'); // 建立 Person这个类的反射类  
    $instance  = $class->newInstanceArgs($args); // 相当于实例化Person 类 

    1)获取属性(Properties)

    $properties = $class->getProperties();
    foreach ($properties as $property) {
     echo $property->getName() . "
    ";
    }
    // 输出:
    // _allowDynamicAttributes
    // id
    // name
    // biography

    默认情况下,ReflectionClass会获取到所有的属性,private 和 protected的也可以。如果只想获取到private属性,就要额外传个参数:

    $private_properties = $class->getProperties(ReflectionProperty::IS_PRIVATE);

    可用参数列表:

    ReflectionProperty::IS_STATIC
    ReflectionProperty::IS_PUBLIC
    ReflectionProperty::IS_PROTECTED
    ReflectionProperty::IS_PRIVATE

    2)获取注释

    通过getDocComment可以得到写给property的注释。

    foreach ($properties as $property) {
     if ($property->isProtected()) {
      $docblock = $property->getDocComment();
      preg_match('/ type=([a-z_]*) /', $property->getDocComment(), $matches);
      echo $matches[1] . "
    ";
     }
    }
    // Output:
    // primary_autoincrement
    // varchar
    // text

    3)获取类的方法

    getMethods()       来获取到类的所有methods。
    hasMethod(string)  是否存在某个方法
    getMethod(string)  获取方法

    4)执行类的方法

    $instance->getName(); // 执行Person 里的方法getName
    // 或者:
    $method = $class->getmethod('getName'); // 获取Person 类中的getName方法
    $method->invoke($instance);    // 执行getName 方法
    // 或者:
    $method = $class->getmethod('setName'); // 获取Person 类中的setName方法
    $method->invokeArgs($instance, array('xxxx.com'));

    二、通过ReflectionMethod,我们可以得到Person类的某个方法的信息:

    • 1.是否“public”、“protected”、“private” 、“static”类型
    • 2.方法的参数列表
    • 3.方法的参数个数
    • 4.反调用类的方法
    // 执行detail方法
    $method = new ReflectionMethod('Person', 'test');
     
    
    if ($method->isPublic() && !$method->isStatic()) {
     echo 'Action is right';
    }
    echo $method->getNumberOfParameters(); // 参数个数
    echo $method->getParameters(); // 参数对象数组

    当然还有很多的功能和方法这里就不一一赘述了,大家可以去官方文档查看,发挥自己的想象力~,抛砖引玉~~

    附录:

    ReflectionClass::__construct — 初始化 ReflectionClass 类
    ReflectionClass::export — 导出一个类
    ReflectionClass::getConstant — 获取定义过的一个常量
    ReflectionClass::getConstants — 获取一组常量
    ReflectionClass::getConstructor — 获取类的构造函数
    ReflectionClass::getDefaultProperties — 获取默认属性
    ReflectionClass::getDocComment — 获取文档注释
    ReflectionClass::getEndLine — 获取最后一行的行数
    ReflectionClass::getExtension — 根据已定义的类获取所在扩展的 ReflectionExtension 对象
    ReflectionClass::getExtensionName — 获取定义的类所在的扩展的名称
    ReflectionClass::getFileName — 获取定义类的文件名
    ReflectionClass::getInterfaceNames — 获取接口(interface)名称
    ReflectionClass::getInterfaces — 获取接口
    ReflectionClass::getMethod — 获取一个类方法的 ReflectionMethod。
    ReflectionClass::getMethods — 获取方法的数组
    ReflectionClass::getModifiers — 获取类的修饰符
    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::isInstance — 检查类的实例
    ReflectionClass::isInstantiable — 检查类是否可实例化
    ReflectionClass::isInterface — 检查类是否是一个接口(interface)
    ReflectionClass::isInternal — 检查类是否由扩展或核心在内部定义
    ReflectionClass::isIterable — Check whether this class is iterable
    ReflectionClass::isIterateable — 检查是否可迭代(iterateable)
    ReflectionClass::isSubclassOf — 检查是否为一个子类
    ReflectionClass::isTrait — 返回了是否为一个 trait
    ReflectionClass::isUserDefined — 检查是否由用户定义的
    ReflectionClass::newInstance — 从指定的参数创建一个新的类实例
    ReflectionClass::newInstanceArgs — 从给出的参数创建一个新的类实例。
    ReflectionClass::newInstanceWithoutConstructor — 创建一个新的类实例而不调用它的构造函数
    ReflectionClass::setStaticPropertyValue — 设置静态属性的值
    ReflectionClass::__toString — 返回 ReflectionClass 对象字符串的表示形式。

    作者:旧旧的 <393210556@qq.com> 解决问题的方式,就是解决它一次

  • 相关阅读:
    移动端页面使用rem布局
    Vue2.0 render:h => h(App)
    sublime3 快捷键大全
    node-webkit学习之【无边框窗口用JS实现拖动改变大小等】
    mock.js-无需等待,随机产生数据,让前端独立于后端进行开发
    Sublime Text 3 遇到的一些小坑的解决方法
    Angular Cli 升级到最新版本
    Angular 表单嵌套、动态表单
    angular6 iframe应用
    JS 时间格式 相互转化
  • 原文地址:https://www.cnblogs.com/widgetbox/p/11096280.html
Copyright © 2020-2023  润新知