您好,欢迎访问一九零五行业门户网

php反射机制讲解

php 5 具有完整的反射 api,添加了对类、接口、函数、方法和扩展进行反向工程的能力。 此外,反射 api 提供了方法来取出函数、类和方法中的文档注释。
tp框架中反射的使用:reflectionclass::__construct — 构造一个 reflectionclass 类
public reflectionclass::__construct ( mixed $argument )
reflectionclass::newinstanceargs — 从给出的参数创建一个新的类实例,给出的参数将传递到类的构造函数。
public object reflectionclass::newinstanceargs ([ array $args ] )
/thinkphp/library/think/container.php
 /**      * 调用反射执行类的实例化 支持依赖注入      * @access public      * @param  string    $class 类名      * @param  array     $vars  参数      * @return mixed      */     public function invokeclass($class, $vars = [])     {         try {            $reflect = new reflectionclass($class);            $constructor = $reflect->getconstructor();            //用于支持依赖的注入             $args = $constructor ? $this->bindparams($constructor, $vars) : [];            return $reflect->newinstanceargs($args);         } catch (reflectionexception $e) {            throw new classnotfoundexception('class not exists: ' . $class, $class);         }     }
相关推荐:
详解php反射方法调用执行类中的私有方法
简述php反射机制实例详解
php反射机制详以及插件架构实例详解
以上就是php反射机制讲解的详细内容。
其它类似信息

推荐信息