zoukankan      html  css  js  c++  java
  • PHP 反射类学习记录

    原文:http://www.upwqy.com/details/58.html

    1 开发环境

    windows 

    TP5

    参考文档  

    http://php.net/manual/zh/class.reflectionclass.php

    2 准备工作 

    新建一个测试反射类 TestReflection.php

    <?php
    /**
     * User: [一秋]
     * Date: 2017-11-23
     * Time: 16:29
     * Desc: 成功来源于点滴
     */
    
    namespace appapicontrollerv3;
    
    
    class TestReflection
    {
        /**
         * @title 首页
         */
        public function index(){
            echo 'index';
        }
        public function add(){
            echo 'add';
        }
    
    }

     在新建 一个类  Test.php

    3 测试

    3.1 ReflectionClass::__construct — 初始化 ReflectionClass 类

    public ReflectionClass::__construct ( mixed $argument )

    public function index(){
        $method = new ReflectionClass('appapicontrollerv3TestReflection');
        dump($method);
    }

    返回结果是 

    object(ReflectionClass)#12 (1) {
      ["name"] => string(26) "appapicontrollerv3TestReflection"
    }

    3.2 ReflectionClass::export — 导出一个类

    public static string ReflectionClass::export ( mixed $argument [, bool $return = false ] )

    public function index(){
        $method = new ReflectionClass('ReflectionClass');
        Reflection::export($method);
    
    }

    返回的结果是:

    Class [ class appapicontrollerv3TestReflection ] { @@ D:phpStudyWWWmmksapplicationapicontrollerv3TestReflection.php 12-21 - Constants [0] { } - Static properties [0] { } - Static methods [0] { } - Properties [0] { } - Methods [2] { Method [ public method index ] { @@ D:phpStudyWWWmmksapplicationapicontrollerv3TestReflection.php 14 - 16 } Method [ public method add ] { @@ D:phpStudyWWWmmksapplicationapicontrollerv3TestReflection.php 17 - 19 } } }

    3.3 ReflectionClass::getConstant — 获取定义过的一个常量

    public mixed ReflectionClass::getConstant ( string $name )

    获取定义过的一个常量。

    在TestReflection 新增一个常量

    <?php
    /**
     * User: [一秋]
     * Date: 2017-11-23
     * Time: 16:29
     * Desc: 成功来源于点滴
     */
    namespace appapicontrollerv3;
    class TestReflection
    {
        const NAME = 'wang';
        /**
         * @title 首页
         */
        public function index(){
            echo 'index';
        }
        public function add(){
            echo 'add';
        }
    
    }
     

    Test.php

    <?php
    /**
     * User: [一秋]
     * Date: 2017-11-23
     * Time: 15:59
     * Desc: 成功来源于点滴
     */
    
    namespace appapicontrollerv3;
    
    class Test
    {
        public function index(){
            $method = new ReflectionClass('appapicontrollerv3TestReflection');
            //导出一个类
    //        Reflection::export($method);
            $name = $method->getConstant('NAME');
            dump($name);
        }
    }

    返回的结果是 

    string(4) "wang"

    这里如果常量不存在 则返回false 

    3.4 ReflectionClass::getConstants — 获取一组常量

    public array ReflectionClass::getConstants ( void )

    这里的 getConstants 不要拼错了。比上一个方法多了一个 s  

    获取某个类的全部已定义的常量,不管可见性如何定义。

    本函数还未编写文档,仅有参数列表。

    <?php
    /**
     * User: [一秋]
     * Date: 2017-11-23
     * Time: 15:59
     * Desc: 成功来源于点滴
     */
    
    namespace appapicontrollerv3;
    
    class Test
    {
        public function index(){
            $method = new ReflectionClass('appapicontrollerv3TestReflection');
            //导出一个类
    //        Reflection::export($method);
            $name = $method->getConstants();
            dump($name);
        }
    }

    返回的结果是

    array(1) {
      ["NAME"] => string(4) "wang"
    }

    3.5 ReflectionClass::getConstructor — 获取类的构造函数

    public ReflectionMethod ReflectionClass::getConstructor ( void )

    获取已反射的类的构造函数。

    为了方便测试  这里映射自身

    <?php
    /**
     * User: [一秋]
     * Date: 2017-11-23
     * Time: 15:59
     * Desc: 成功来源于点滴
     */
    
    namespace appapicontrollerv3;
    
    class Test
    {
        const NAME = 'wang';
        public function __construct()
        {
        }
    
        public function index(){
            $method = new ReflectionClass(__CLASS__);
            //导出一个类
    //        Reflection::export($method);
            $name = $method->getConstructor();
            dump($name);
        }
    }
     

    返回的结果是

    object(ReflectionMethod)#13 (2) {
      ["name"] => string(11) "__construct"
      ["class"] => string(26) "appapicontrollerv3Test"
    }

    3.6 ReflectionClass::getDefaultProperties — 获取默认属性

    public array ReflectionClass::getDefaultProperties ( void )

    获取类的默认属性(包括了继承的属性)。

    参数

    此函数没有参数。

    返回值

    默认属性的数组,其键是属性的名称,其值是属性的默认值或者在属性没有默认值时是 NULL。 这个函数不区分静态和非静态属性,也不考虑可见性修饰符。

    <?php
    /**
     * User: [一秋]
     * Date: 2017-11-23
     * Time: 15:59
     * Desc: 成功来源于点滴
     */
    
    namespace appapicontrollerv3;
    
    /**
     * 反射测试类
     * Class Test
     * @package appapicontrollerv3
     */
    class Test extends par
    {
        public $user = 'user';
        private $name = 'name';
        public static $tel = '12345678912';
        public $pass;
    
        const NAME = 'wang';
    
        /**
         * 构造函数
         * Test constructor.
         */
        public function __construct()
        {
        }
    
        /**
         * 测试入口
         * @param int $id 没有id
         */
        public function index(){
            $method = new ReflectionClass(__CLASS__);
            $name = $method->getDefaultProperties();
            dump($name);
        }
    }
    class par{
        protected $par = 'par';
    }
     

    返回的结果是

    array(5) {
      ["tel"] => string(11) "12345678912"
      ["user"] => string(4) "user"
      ["name"] => string(4) "name"
      ["pass"] => NULL
      ["par"] => string(3) "par"
    }

    3.7 ReflectionClass::getDocComment — 获取文档注释

    public string ReflectionClass::getDocComment ( void )

    从一个类中获取文档注释。

    Warning

    本函数还未编写文档,仅有参数列表。

    参数

    此函数没有参数。

    返回值

    如果存在则返回文档注释,否则返回 FALSE

    <?php
    /**
     * User: [一秋]
     * Date: 2017-11-23
     * Time: 15:59
     * Desc: 成功来源于点滴
     */
    
    namespace appapicontrollerv3;
    
    /**
     * 反射测试类
     * Class Test
     * @package appapicontrollerv3
     */
    class Test extends par
    {
        public $user = 'user';
        private $name = 'name';
        public static $tel = '12345678912';
        public $pass;
    
        const NAME = 'wang';
    
        /**
         * 构造函数
         * Test constructor.
         */
        public function __construct()
        {
        }
    
        /**
         * 测试入口
         * @param int $id 没有id
         */
        public function index(){
            $method = new ReflectionClass(__CLASS__);
            $name = $method->getDocComment();
            dump($name);
        }
    }
    
    /**
     * Class par
     * @package appapicontrollerv3
     */
    class par{
        protected $par = 'par';
    }

    返回结果是

    string(78) "/**
     * 反射测试类
     * Class Test
     * @package appapicontrollerv3
     */"

    3.8 ReflectionClass::getEndLine — 获取最后一行的行数

    public int ReflectionClass::getEndLine ( void )

    从用户定义的类获取其最后一行的行数。

    参数

    此函数没有参数。

    返回值

    返回用户定义的类最后一行的行数,如果未知则返回 FALSE

    
    
        /**
         * 测试入口
         * @param int $id 没有id
         */
        public function index(){
            $method = new ReflectionClass(__CLASS__);
            $name = $method->getEndLine();
            dump($name);
        }


    返回结果是

    int(39)

    3.9 ReflectionClass::getExtension — 根据已定义的类获取所在扩展的 ReflectionExtension 对象

    public ReflectionExtension ReflectionClass::getExtension ( void )

    获取已定义类的扩展的 ReflectionExtension 对象。

    参数

    此函数没有参数。

    返回值

    类所处的扩展的 ReflectionExtension 对象的表示,如果是用户定义的类则返回 NULL

        /**
         * 测试入口
         * @param int $id 没有id
         */
        public function index(){
            $method = new ReflectionClass('Reflection');
            $name = $method->getExtension();
            dump($name);
        }

    返回的结果是

    object(ReflectionExtension)#13 (1) {
      ["name"] => string(10) "Reflection"
    }

    3.10  ReflectionClass::getExtensionName — 获取定义的类所在的扩展的名称

    说明

    public string ReflectionClass::getExtensionName ( void )

    获取定义的类所在的扩展的名称。

    参数

    此函数没有参数。

    返回值

    获取定义的类所在的扩展的名称,如果是用户定义的类,则返回 FALSE

    public function index(){
        $method = new ReflectionClass('ReflectionClass');
        $name = $method->getExtensionName();
        dump($name);
    }

    返回的结果是

    string(10) "Reflection"
  • 相关阅读:
    Tkinter之Label部件
    Tkinter编码风格
    GUI之tkinter视窗设计模块
    绘制函数图像
    backbone学习总结(一)
    实习两个月,写在辞职的今天
    Spark SQL 编程初级实践2- 编程实现利用 DataFrame 读写 MySQL 的数据
    Spark SQL 编程初级实践1-Spark SQL 基本操作
    spark创建DataFrame的几种方式
    python-with open() as file相关参数以及常用打开方式
  • 原文地址:https://www.cnblogs.com/wqy415/p/7890116.html
Copyright © 2011-2022 走看看