我想在使用縮短命名空間路徑的同時創建一個帶有參數的對象以加載函數。它是這樣,創建具有變量類名稱和名稱空間的對象
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