• PHP---反射


    • 所谓反射就是定义好一个类之后,通过ReflectionClass读取类的名字从而获取类结构信息的过程
    class mycoach
    {
        protected $name;
        protected $age;
        public function __construct($name,$age)
        {
            $this->name = $name;
            $this->age = $age;
        }
        public function eat()
        {
            echo "嗯,吃点增加体能的东西".PHP_EOL;
        }
    }
    • 拿到一个反射对象
    $coach = new mycoach('陈培昌',22);
    $a = new ReflectionClass('mycoach');
    if($a->isInstantiable())
    {
    echo "科学的饮食和训练".PHP_EOL;
    }

    $myattributes = $a->getConstructor();
    var_dump($myattributes);

    输出结果:

    科学的饮食和训练
    object(ReflectionMethod)#3 (2) {
      ["name"]=>
      string(11) "__construct"
      ["class"]=>
      string(7) "mycoach"
    }
    • 方法一  getProperties() 注意!要想实现两个属性的打印,务必在类结构体中声明两个保护属性 (例如:protected $name)
    $property = $a->getProperties();
    print('=================property================='.PHP_EOL);
    var_dump($property);

    输出结果:

    =================property=================
    array(2) {
      [0]=>
      object(ReflectionProperty)#4 (2) {
        ["name"]=>
        string(4) "name"
        ["class"]=>
        string(7) "mycoach"
      }
      [1]=>
      object(ReflectionProperty)#5 (2) {
        ["name"]=>
        string(3) "age"
        ["class"]=>
        string(7) "mycoach"
      }
    }
    • 方法二 getMethods() getname()
    print('=================methods================='.PHP_EOL);
    var_dump($a->getMethods());
    print('=================methods by row================='.PHP_EOL);
    
    foreach($a->getMethods() as $method)
    {
        echo $method->getName().PHP_EOL;
    }

    输出结果:

    =================methods=================
    array(2) {
      [0]=>
      object(ReflectionMethod)#6 (2) {
        ["name"]=>
        string(11) "__construct"
        ["class"]=>
        string(7) "mycoach"
      }
      [1]=>
      object(ReflectionMethod)#7 (2) {
        ["name"]=>
        string(3) "eat"
        ["class"]=>
        string(7) "mycoach"
      }
    }
    =================methods by row=================
    __construct
    eat
    • hasMethod() getMethod()
    print('=================execute================='.PHP_EOL);
    
    if($a->hasMethod('eat'))
    {
        $method= $a->getMethod('eat');
        print_r($method);
    }

    输出结果:

    =================execute=================
    ReflectionMethod Object
    (
        [name] => eat
        [class] => mycoach
    )
    
    Process finished with exit code 0
    • 复制粘贴---快速测试
    <?php
    class mycoach
    {
        protected $name;
        protected $age;
        public function __construct($name,$age)
        {
            $this->name = $name;
            $this->age = $age;
        }
        public function eat()
        {
            echo "嗯,吃点增加体能的东西".PHP_EOL;
        }
    }
    $coach = new mycoach('陈培昌',22);
    $a = new ReflectionClass('mycoach');
    if($a->isInstantiable())
    {
        echo "科学的饮食和训练".PHP_EOL;
    }
    $myattributes = $a->getConstructor();
    var_dump($myattributes);
    $property = $a->getProperties();
    print('=================property================='.PHP_EOL);
    var_dump($property);
    print('=================methods================='.PHP_EOL);
    var_dump($a->getMethods());
    print('=================methods by row================='.PHP_EOL);
    
    foreach($a->getMethods() as $method)
    {
        echo $method->getName().PHP_EOL;
    }
    print('=================execute================='.PHP_EOL);
    
    if($a->hasMethod('eat'))
    {
        $method= $a->getMethod('eat');
        print_r($method);
    }
  • 相关阅读:
    linux svn切换用户
    解决QQ“抱歉,无法发起临时会话,您可以 添加对方为好友以发送消息”
    node.js中http.respone.end方法概述
    niginx:duplicate MIME type "text/html" in nginx.conf 错误(转载)
    大三暑假实习,我们可以怎么做
    一个Java程序员的实习总结(2)
    正式工作的前奏——一个Java程序员的实习总结(1)
    【个人】当我秀智商的时候我秀什么
    基于java的设计模式入门(1)——为什么要学习设计模式
    【个人】我不愿让你一个人
  • 原文地址:https://www.cnblogs.com/saintdingspage/p/11588609.html
Copyright © 2020-2023  润新知