在 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;
区别在哪里?资料也是查找不到的。求助
============================ 问题已解决,答案如下 ======================================
前导的反斜杠是不必要的也不推荐的,因为导入的名称必须是完全限定的,不会根据当前的命名空间作相对解析。
As shown in the picture:
![](http://img.php.cn/upload/image/000/000/000/2293c776bc8436a5afd4d4057530e8e5-0.png)
Attached is the address: http://php.net/manual/zh/lang...
Attached text:
So, there is no difference, and it is not recommended to write root''.
@wujunze
@anonymous66
@corki
use
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 Tooltool
starts from the root namespace of the projectuse Tooltool
starts from the namespace in the current directory