douqi1212
2017-07-24 16:41
浏览 127
已采纳

使用变量类名和命名空间创建对象

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" ?

图片转代码服务由CSDN问答提供 功能建议

我正在尝试创建一个带有参数的对象来加载函数,同时在其中使用缩短命名空间路径。 它就像,

 使用Com \ Core \ Service \ Impl作为Impl; 
 
类加载{
公共静态函数服务(String $ class,array $ params)  = array()){
 try {
 $ ucfirstclass = ucfirst($ class); 
 if(interface_exists('\\ Com \\ Core \\ Service \\'。$ ucfirstclass)){
 $ ref  =“Impl \\”。$ ucfirstclass; 
返回new $ ref(); 
} else {
 throw new Exception(“找不到名称$ class的服务”); 
} 
} catch(
)  Throwable $ ex){
 echo $ ex-> getMessage(); 
} 
} 
} 
   
 
 

调用它时,

  $ userService = Load :: service(“user”);  
   
 
 

它抛出异常

  Class'Impl \ User'not found 
    
 
 

如果我只是用完整路径“Com \ Core \ Service \ Impl”替换Load :: service()实现中的“Impl”,它将正常工作。

我是新手。 有人可以帮助我为什么不能使用缩短路径“Com \ Core \ Service \ Impl as Impl”?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • drslez4322 2017-07-24 18:00
    已采纳

    while using shorten namespace path in it.

    There is no such thing as "short namespace". A namespace or a class is determined by its complete path, starting from the root namespace.

    use Com\Core\Service\Impl as Impl;
    

    Impl in the above fragment of code is a class or namespace alias. An alias is resolved at the compile time and it is valid only in the file where it is declared.

    It is not possible to use an alias during the runtime. The only way to refer to a class name during runtime is to generate its absolute path (starting from the root namespace).
    You already discovered this.

    Read more about namespace aliases/importing.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • drd2551 2017-07-24 17:10

    When referring to class names as strings, you always have to use the fully-qualified class name.

    Try this:

    $ucfirstclass = ucfirst($class);
    
    if (interface_exists('Com\\Core\\Service\\' . $ucfirstclass)) {
        $ref = 'Com\\Core\\Service\\Impl\\' .$ucfirstclass;
    
        return new $ref();
    }
    

    For reference, see:

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题