PHP 反射类的简单使用!

  公司项目中用到了反射类,它的作用是可以把灵活的传过来的类名通过映射里面的方法实现框架模块搭建!使用方法总结为下面几步:

    第一步:

       //传类名过来映射
            $this->_configClass = new ReflectionClass($this->_configClassName);
            //得到这个类的实例
            $this->_configInstance = $this->_configClass->newInstanceArgs();

    第二步:

       //检测这个类中是否含有这个方法:hasMethod(),注意是原类!如果有则获取它:getMethod(),然后通过原类实例
       化出来的类用:invoke(参数为前面实例化得到的新类)调用即可!
       $this->_includeSourseConfig = ($this->_configClass->hasMethod('_includeSourse')) ? $this->_configClass->getMethod('_includeSourse')->invoke($this->_configInstance) : false;

    这样就搞定了。

原文地址:https://www.cnblogs.com/lgqtecng/p/6534282.html