• PHP反射


    PHP5 具有完整的反射API,添加对类、接口、函数、方法和扩展进行反向工程的能力。

    反射是什么

    反射(Reflection)是指在PHP运行状态中,扩展分析PHP程序,导出或提取出关于方法属性参数等的详细信息,包括注释。这种动态获取的信息以及动态调用对象的方法的功能称为反射API。反射是操纵面向对象范型中元模型的API,其功能十分强大,可帮助我们构建复杂,可扩展的应用。

    其用途如:自动加载插件,自动生成文档,甚至可用来扩充PHP语言。

    PHP反射api由若干类组成,可帮助我们用来访问程序的元数据或者同相关的注释交互。借助反射我们可以获取诸如类实现了那些方法,创建一个类的实例(不同于用new创建),调用一个方法(也不同于常规调用),传递参数,动态调用类的静态方法。

    反射api是PHP内建的OOP技术扩展,包括一些类,异常和接口,综合使用他们可用来帮助我们分析其它类,接口,方法,属性,方法和扩展。这些OOP扩展被称为反射。

    平常我们用的比较多的是 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

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

    常量 Contants
    属性 Property Names
    方法 Method Names静态
    属性 Static Properties
    命名空间 Namespace
    
    Person类是否为final或者abstract
    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
    

    通过$property->getName()可以得到属性名。

    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('snsgou.com'));
    

    ReflectionMethod

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

    是否publicprotectedprivatestatic类型
    方法的参数列表
    方法的参数个数
    反调用类的方法

    // 执行detail方法
    $method = new ReflectionMethod('Person', 'test');
    
    if ($method->isPublic() && !$method->isStatic()) {
        echo 'Action is right';
    }
    echo $method->getNumberOfParameters(); // 参数个数
    echo $method->getParameters(); // 参数对象数组
    

    实践中的应用

    1、参数校验

    function checkMethodParams($class, $method, $params){
    	$method = new ReflectionMethod(get_class($class), $method);
    	$m_params = $method->getParameters();
    
    	$m_params_require_num = 0;
    	foreach ($m_params as $param) {
    		if (!$param->isOptional()) {
    			$m_params_require_num += 1;
    		}
    	}
    
    	if ($m_params_require_num > count($params)) {
    		throw new HException('缺少参数');
    	}
    }
    

    2、获取一个类或扩展的内部详细信息

    <?php
    $extension = 'pdo';
    $e = new ReflectionExtension($extension);
    print "<?php
    
    // {$extension} Version: " . $e->getVersion() . "
    
    ";
    foreach ($e->getClasses() as $c) {
      print 'class ' . $c->name . " {
    ";
      foreach ($c->getMethods() as $m) {
        print '  ';
        if ($m->isPublic()) {
            print 'public';
        } elseif ($m->isProtected()) {
            print 'protected';
        } elseif ($m->isPrivate()) {
            print 'private';
        }
        print ' function ' . $m->name . '(';
        $sep = '';
        foreach ($m->getParameters() as $p) {
          print $sep;
          $sep = ', ';
          if ($p->isOptional())
            print '$' . $p->name . ' = null' ;
          else
            print '$' . $p->name;
        }
        print "){}
    ";
      }
      print "}
    
    ";
    }
    
    

    输出:

    <?php
    
    // pdo Version: 1.0.4dev
    
    class PDOException {
      private function __clone(){}
      public function __construct($message = null, $code = null, $previous = null){}
      public function getMessage(){}
      public function getCode(){}
      public function getFile(){}
      public function getLine(){}
      public function getTrace(){}
      public function getPrevious(){}
      public function getTraceAsString(){}
      public function __toString(){}
    }
    
    class PDO {
      public function __construct($dsn, $username = null, $passwd = null, $options = null){}
      public function prepare($statement, $options = null){}
      public function beginTransaction(){}
      public function commit(){}
      public function rollBack(){}
      public function inTransaction(){}
      public function setAttribute($attribute, $value){}
      public function exec($query){}
      public function query(){}
      public function lastInsertId($seqname = null){}
      public function errorCode(){}
      public function errorInfo(){}
      public function getAttribute($attribute){}
      public function quote($string, $paramtype = null){}
      public function __wakeup(){}
      public function __sleep(){}
      public function getAvailableDrivers(){}
    }
    
    class PDOStatement {
      public function execute($bound_input_params = null){}
      public function fetch($how = null, $orientation = null, $offset = null){}
      public function bindParam($paramno, $param, $type = null, $maxlen = null, $driverdata = null){}
      public function bindColumn($column, $param, $type = null, $maxlen = null, $driverdata = null){}
      public function bindValue($paramno, $param, $type = null){}
      public function rowCount(){}
      public function fetchColumn($column_number = null){}
      public function fetchAll($how = null, $class_name = null, $ctor_args = null){}
      public function fetchObject($class_name = null, $ctor_args = null){}
      public function errorCode(){}
      public function errorInfo(){}
      public function setAttribute($attribute, $value){}
      public function getAttribute($attribute){}
      public function columnCount(){}
      public function getColumnMeta($column){}
      public function setFetchMode($mode, $params = null){}
      public function nextRowset(){}
      public function closeCursor(){}
      public function debugDumpParams(){}
      public function __wakeup(){}
      public function __sleep(){}
    }
    
    class PDORow {
    }
    
    

    3、实现代理模式

    <?php
    /**
     * Created by PhpStorm.
     * User: YJC
     * Date: 2016/6/9 009
     * Time: 17:56
     */
    
    class Mysql{
    
        public function query($sql){
            echo $sql ."<br/>";
        }
    
    }
    
    
    class Proxy{
    
        private $obj;
    
        public function __construct($obj)
        {
            $this->obj = new $obj;
        }
    
        public function __call($name, $args){
            $reflec = new ReflectionClass($this->obj);
            if($method = $reflec->getMethod($name)){
                if($method->isPrivate()){
                    throw new Exception("$method 方法不能直接调用!");
                }
    
                $this->selectDB($args);
                $this->beforeFilter($args);
                $method->invoke($this->obj, $args[0]);
                $this->afterFilter($args);
            }
        }
    
        public function selectDB($args){
            $sql = $args[0];
    
            $type = '';
            if(stripos($sql, 'select') === 0){
                $type = 'query';
                echo '查询<br/>';
            }elseif(stripos($sql, 'insert') === 0 || stripos($sql, 'update') === 0 || stripos($sql, 'delete') === 0){
                $type = 'exec';
                echo '更新<br/>';
            }
        }
    
        public function beforeFilter($args){
            echo '前置过滤<br/>';
        }
    
        public function afterFilter($args){
            echo '后置过滤<br/>';
        }
    }
    
    
    //$obj = new Mysql();
    $obj = new Proxy('Mysql');
    $obj->query('insert * from user');
    

    参考:
    [PHP手册] ReflectionClass类
    [PHP手册] ReflectionMethod类

  • 相关阅读:
    学习笔记: yield迭代器
    学习笔记: 委托解析和封装,事件及应用
    学习笔记: MD5/DES/RSA三类加密,SSL协议解析
    学习笔记: Expression表达式目录树详解和扩展封装
    学习笔记: Expression表达式目录树详解和扩展封装
    学习笔记: IO操作及序列化
    数据类型转换
    短路运算(逻辑运算是短路运算中最常见的一种)
    清除浮动
    css初始化
  • 原文地址:https://www.cnblogs.com/52fhy/p/5517075.html
Copyright © 2020-2023  润新知