[TOC] ## demo ``` //image.php use \App\Lib\Upload; class Image{ public function __construct($param){ $this->param=$param; } } //run 初始化 image 类,并传入参数 $param=[]; (new \ReflectionClass("\App\Lib\Upload\Image"))->newInstanceArgs($param); ``` ## 应用 ### 生成工厂类 ``` class ClassArr { public function initClass($type, $supportedClass,$params=[],$needInstance=true){ if (!array_key_exists($type, $supportedClass)){ return false; } $className = $supportedClass[$type]; return $needInstance?(new \ReflectionClass($className))->newInstanceArgs($params):$className; } static public function uploadClassStat(){ return[ 'image'=>'\App\Lib\Upload\Image', 'video'=>'\App\Lib\Upload\Video', ]; } } ``` run ``` $classArr = new ClassArr(); $uploadObj = $classArr->initClass("image", ClassArr::uploadClassStat(),[$request]); $uploadObj->upload();//执行类中的方法 ```