zoukankan      html  css  js  c++  java
  • 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!!!
  • 相关阅读:
    WPF中的句柄
    WPF中的焦点问题
    Vue项目(一):VSCode环境开发Vue程序以及其中遇到的问题
    C#实现三种方式的模拟按键
    c++温故之结构体写法
    WPF搜索框
    vue框架学习
    Git连接失败问题解决方案
    单击双击冲突解决 小程序
    uniapp 微信小程序 wx.createAnimation 实现向上滚动弹幕
  • 原文地址:https://www.cnblogs.com/longfeiPHP/p/5541941.html
Copyright © 2011-2022 走看看