I am trying to create an object with a parameter to loader function while using shorten namespace path in it. It goes like,
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();
}
}
}
While calling it like,
$userService = Load::service("user");
it is throwing an exception
Class 'Impl\User' not found
Though it'll work fine if I'll just replace "Impl" inside Load::service() implementation with full path "Com\Core\Service\Impl".
I'm new with this. Can someone help here why can't I use shorten path "Com\Core\Service\Impl as Impl" ?