PHP 中命名空间 use 关键字的疑惑
淡淡烟草味
淡淡烟草味 2017-05-16 13:13:36
0
4
561

在 PHP 5.3之后加入了use来引用命名空间,
然后就出现了比如:

use Tool\tool; new tool();

这样简化的操作。

然而,我知道不使用use,命名空间的三种被调用方式:

1、非限定方式 new tool() 2、限定方式 new Tool\tool() 3、完全限定方式 new \Tool\tool()

现在我理解了不需要use引入命名空间的操作方式,即上面3中方式。那么问题就是:

use Tool\tool; 与 use \Tool\tool;

区别在哪里?资料也是查找不到的。求助

============================ 问题已解决,答案如下 ======================================

前导的反斜杠是不必要的也不推荐的,因为导入的名称必须是完全限定的,不会根据当前的命名空间作相对解析。

淡淡烟草味
淡淡烟草味

reply all (4)
洪涛

As shown in the picture:

Attached is the address: http://php.net/manual/zh/lang...

Attached text:

Note that for names in namespaces (fully qualified names that include namespace delimiters such as FooBar and relative global names that do not include namespace delimiters such as FooBar), the leading backslash is unnecessary and unavailable. Recommended, because imported names must be fully qualified and will not be resolved relative to the current namespace.

So, there is no difference, and it is not recommended to write root''.

@wujunze
@anonymous66
@corki

    刘奇

    use

    Leading backslashes are unnecessary and not recommended because imported names must be fully qualified and will not be resolved relative to the current namespace.

      仅有的幸福

      I think it can be understood that the qualified method and the non-qualified method can be understood as the difference between the relative path when we import the file and the absolute path when we import the file. They are just a bit the same. I don’t know if I understand it correctly. Please correct me. Thanks!!!

        洪涛

        use Tooltoolstarts from the root namespace of the project

        use Tooltoolstarts from the namespace in the current directory

          Latest Downloads
          More>
          Web Effects
          Website Source Code
          Website Materials
          Front End Template
          About us Disclaimer Sitemap
          php.cn:Public welfare online PHP training,Help PHP learners grow quickly!