我想在使用缩短命名空间路径的同时创建一个带有参数的对象以加载函数。它是这样,创建具有变量类名称和名称空间的对象
use Com\Core\Service\Impl as Impl;
class Load {
public static function service(String $class, array $params = array()){
try {
$ucfirstclass = ucfirst($class);
if (interface_exists('\\Com\\Core\\Service\\' . $ucfirstclass)) {
$ref = "Impl\\".$ucfirstclass;
return new $ref();
} else {
throw new Exception("Service with name $class not found");
}
} catch (\Throwable $ex) {
echo $ex->getMessage();
}
}
}
在呼吁像,
$userService = Load::service("user");
它抛出一个异常
Class 'Impl\User' not found
虽然它会正常工作,如果我只是取代 “默认地将Impl”使用完整路径“Com \ Core \ Service \ Impl”实现Load :: service()内部的实现。
我是新来的。有人可以帮助我,为什么我不能使用缩短路径“Com \ Core \ Service \ Impl as Impl”?
有道理。感谢您的解释。 – nks