zoukankan      html  css  js  c++  java
  • PHP中的反射机制,类的反射

    <?php
    
    /**
     * User
     */
    class User
    {
        /**
         * @var $username  mixed 用户明
         * @var $age mixed  年龄
         *
         */
        private $username;
        private $age;
    
        /**
         * User constructor.
         * @param $username
         * @param $age
         */
        function __construct($username, $age)
        {
            $this->age = $age;
            $this->username = $username;
    
        }
    
        public function sayhello()
        {
            echo "heloo";
    
        }
    
        public function sayword($word = "ok")
        {
            echo $word;
        }
    
        /**
         * @return mixed
         */
        public function getAge()
        {
            return $this->age;
        }
    
        /**
         * @return mixed
         */
        public function getUsername()
        {
            return $this->username;
        }
    
    }
    
    $user = new User("ddd", "333");
    
    $ref = new ReflectionClass($user);   //反射类
    $ins = $ref->newInstanceArgs(['ssss', 33]);//反射类方式实例化对象
    //调用方法
    $ins->sayhello();
    echo "-------------";
    $user->sayhello();
    
    echo "<br>";
    
    echo $user->getAge();
    echo "-------------";
    echo $ins->getAge();  //执行累的方法
    
    echo "<br>";
    
    /**
     * 默认情况下,ReflectionClass会获取到所有的属性,private 和 protected的也可以。如果只想获取到private属性,就要额外传个参数:
     * $private_properties = $class->getProperties(ReflectionProperty::IS_PRIVATE);
     * 可用参数列表:
     * ReflectionProperty::IS_STATIC
     * ReflectionProperty::IS_PUBLIC
     * ReflectionProperty::IS_PROTECTED
     * ReflectionProperty::IS_PRIVATE
     * 如果要同时获取public 和private 属性,就这样写:ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED。
     * 通过$property->getName()可以得到属性名。
     */
    $properties = $ref->getProperties();  //获取属性
    var_dump($properties);
    foreach ($properties as $property) {
        echo $property->getName() . "
    ";
    }
    
    /**
     *  通过getDocComment可以得到写给property的注释。
     */
    foreach ($properties as $property) {
        $docblock = $property->getDocComment();
        echo $docblock;
    }
    echo "-----------<br>";
    /**
     * 获取方法(methods):通过getMethods() 来获取到类的所有methods。
     */
    $methods = $ref->getMethods();
    var_dump($methods);
    echo "-----------<br>";
    foreach ($methods as $method) {
        echo "<br>";
        echo $method->getName();
    }
    
    echo "<br>";
    $met = $ref->getMethod('sayword');
    var_dump($met);
    // 也是执行累的方法
    $met->invoke($ins) ;

    PHP 中类的反射的一点用法。

  • 相关阅读:
    使用tcmalloc编译启动时宕机
    使用tcmalloc编译出现undefined reference to `sem_init'
    使用AddressSanitizer做内存分析(一)——入门篇
    VIM-美化你的标签栏
    Entity Framework Code First (六)存储过程
    Entity Framework Code First (五)Fluent API
    Entity Framework Code First (四)Fluent API
    Entity Framework Code First (三)Data Annotations
    Entity Framework Code First (二)Custom Conventions
    Entity Framework Code First (一)Conventions
  • 原文地址:https://www.cnblogs.com/cs88/p/6291224.html
Copyright © 2011-2022 走看看