Confusion about the namespace use keyword in PHP
淡淡烟草味
淡淡烟草味 2017-05-16 13:13:36
0
4
656

After PHP 5.3, use was added to refer to the namespace,
and then something like:

use Tool\tool;

new tool();

Such a simplified operation.

However, I know of three ways a namespace can be called without using use:

1. Unlimited method new tool()
2. Limitation method new Tool\tool()
3. Fully qualified method new \Tool\tool()

Now I understand the operation method that does not require use to introduce the namespace, that is, the above 3 methods. Then the question is:

use Tool\tool;
and
use \Tool\tool;

What's the difference? The information cannot be found either. Help

============================ The problem has been solved and the answer is as follows ============= =========================

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

淡淡烟草味
淡淡烟草味

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 Tooltool starts from the root namespace of the project

use Tooltool starts from the namespace in the current directory

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template