• php反射


    <?php
    /** 
    * A test class
    *
    * @param  foo bar
    * @return baz
    */
    class Person{
        private $_allowDynamicAttributes = false;
        protected $id = 0;
        protected $name;
        protected $biography;
        const ONE = 'number one';
        const TWO = 'number two';
        
        public function __construct(){
            return 'this is construct function';    
        }
        //一些普通方法
        public function getId(){
            return $this->id;
                
        }
        public function setId($v){
            return $this->id = $v;
        }
        public function getName(){
            return $this->name;    
        }
        public function setName($v){
            return $this->name = $v;
        }
        public function getBiography(){
            return $this->biography;    
        }
        public function setBiography($v){
            return $this->biography = $v;    
        }    
    }
    
    $class = new ReflectionClass('Person');//建立反射
    echo '<pre>';
    
    //导出类
    $class->export($class);
    //获取类中某个常量的值
    echo $class->getConstant('ONE');   // print "number one"
    //获取类中所有常量,数组形式
    print_r($class->getConstants());
    //获取类中的构造函数 如果类中不存在构造函数则返回 NULL
    var_dump($class->getConstructor());
    //获取类中的熟悉
    var_dump($class->getDefaultProperties());
    //获取类的注释(注释格式必须按要求去写)
    var_dump($class->getDocComment());
    //获取类的最后一行的行数
    echo $class->getEndLine();
    
    //其他方法详见  http://php.net/manual/zh/book.reflection.php
    
    echo '</pre>';
    ?>

     总结:通过自己对反射的了解,反射就是可以查看类里面的属性及方法的一些特性,主要用到对接口继承和实现的时候,对接口文件不能正常访问的时候。

    If the copyright belongs to the longfei, please indicate the source!!!
  • 相关阅读:
    【Matlab】去除图片周围空白区域(plot subplot)
    使用nbdev进行jupyter项目的开发
    如何绘制符合打印标准的图形
    如何使用Python完成视频的快速剪辑
    如何查看和修改论文图片的打印尺寸
    使用TMUX替代screen工具
    Emacs设置包管理器以及镜像
    Emacs的配置文件
    Emacs Windows的设置
    数据科学新的工具Julia
  • 原文地址:https://www.cnblogs.com/longfeiPHP/p/5541941.html
Copyright © 2020-2023  润新知