• php 反射


    PHP 具有完整的反射 API,增加了内省类、接口、函数、方法和扩展的能力。 此外,反射 API 提供了方法来取出函数、类和方法中的文档注释。

    A.php

    
    
    /**
     * 类的注释
     */
    class A
    {
        public $one = '';
        public $two = '';
    
        //Constructor
        public function __construct()
        {
            //Constructor
        }
    
        //print variable one
        public function echoOne($a,$b)
        {
    
            return "echoOne---- {$a}|{$b}";
        }
    
    
        /**
         * print variable two
        */
        public function echoTwo()
        {
            return "echoTwo\n";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    反射获取相关信息实例

      /**
         * 反射
         * @return void
         */
        public function reflectDemo()
        {
            //Instantiate the object
            $a = new \A();
            var_dump($a);
            // Instantiate the reflection object
            $reflector = new \ReflectionClass($a);
    
            //Now get all the properties from class A in to $properties array
            $properties = $reflector->getProperties();
            var_dump($properties);
    
            $i =1;
            //Now go through the $properties array and populate each property
            foreach($properties as $property)
            {
                //Populating properties
                $a->{$property->getName()}=$i;
                //Invoking the method to print what was populated
                //$a->{"echo".ucfirst($property->getName())}()."\n";
    
                $i++;
            }
    
            // 相当于实例化这个类 等同于上面的 $a
            $instance = $reflector->newInstance();
    //        var_dump($instance);
    
            // 方法一
            //invoke method
            echo $instance->echoTwo();
            // 方法二
            // get method
            $method = $reflector->getMethod('echoOne');
            echo $method->invokeArgs($instance,["abd","eff"]);
    
            // 方法三 没有参数的方法
            $method = $reflector->getMethod('echoTwo');
            echo $method->invoke($instance);
            // 获取所有 method
            $methods = $reflector->getMethods();
            var_dump($methods);
    
            // 获取类注释 方法注释
            var_dump($reflector->getDocComment());
            var_dump($method->getDocComment());
    
            //判断某个方法是不是公共的
            $method = new \ReflectionMethod($a,'echoOne');
            if ($method->isPublic()) {
                echo "method is public";
            }
    
            // 获取方法的参数和参数个数
            var_dump($method->getParameters());
            var_dump($method->getNumberOfParameters());
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
  • 相关阅读:
    面向对象实验六模板
    tensor和numpy相互转换
    pip pip3安装库时都指向python2的库
    七天接手react项目 系列 —— react 起步
    【C语言】C语言从入门到精通 | 第3章 实践与练习以及答案
    【2014NOIP普及组】T4:子矩阵 试题解析
    shell循环
    低代码会使初级码农失业吗?
    【Linux】《Linux命令行与shell脚本编程大全 (第4版) 》笔记-Chapter6-Linux 环境变量
    程序启停分析与进程常用API的使用
  • 原文地址:https://blog.csdn.net/ManNiaoQinFen/article/details/134219441