根据不同类型,实例化不同的类
比写if else 扩展好些,看起来优雅些吧
// 增加一个类型,在这里增加一对键值对
$supportedClass=[
"image" => "\App\Lib\Upload\Image",
"video" => "\App\Lib\Upload\Video",
];
// $type:上传文件的类型
// $request:上传文件的参数
$uploadObj = $classObj->initClass($type, $supportedClass, [$request, $type]);
$file = $uploadObj->upload();
public function initClass($type, $supportedClass = [], $params = [])
{
//拼接要实例化的类的路径,如:\App\Lib\Upload\Image
$className = $supportedClass[$type];
return $needInstance ? (new \ReflectionClass($className))->newInstanceArgs($params) : $className;
}