search

Home  >  Q&A  >  body text

Confusion about the namespace use keyword in PHP

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.

淡淡烟草味淡淡烟草味2823 days ago658

reply all(4)I'll reply

  • PHP中文网

    PHP中文网2017-05-16 13:15:36

    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

    reply
    0
  • 怪我咯

    怪我咯2017-05-16 13:15:36

    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.

    reply
    0
  • 仅有的幸福

    仅有的幸福2017-05-16 13:15:36

    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!!!

    reply
    0
  • PHP中文网

    PHP中文网2017-05-16 13:15:36

    use Tooltool starts from the root namespace of the project

    use Tooltool starts from the namespace in the current directory

    reply
    0
  • Cancelreply