【发布时间】:2017-12-30 08:24:54
【问题描述】:
我正在尝试创建一个带有加载器函数参数的对象,同时在其中使用缩短命名空间路径。就像,
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
虽然我将 Load::service() 实现中的“Impl”替换为完整路径“Com\Core\Service\Impl”,但它会正常工作。
我是新手。有人可以在这里帮忙,为什么我不能使用缩短路径“Com\Core\Service\Impl as Impl”?
【问题讨论】:
标签: php class object namespaces php-7