Phpnamespaceuse namespace (ns ). After defining an ns, the class, interface, and const (excluding variable) stated below are all in the "domain" of the declared ns. When you reference an included ns file and want to call the content in this ns, you must adjust the current script to this ns domain. Otherwise, you must use the full name () to include the full name of ns ):
[Php]View plain copy
-
// Inc. php
-
Namespace Foo;
-
Class Bar {}
-
-
// The first method to access Foo.
-
Require 'Inc. php ';
-
$ Foo = new \ Foo \ Bar ();
-
-
// Method 2 for accessing Foo
-
Namespace Foo; // adjust the current script to the Foo ns domain, and the namespace statement must be in the first sentence
-
Require 'Inc. php ';
-
$ Foo = new Bar ();
The purpose of the use keyword is to use the ns alias:
[Php]View plain copy
-
// For example
-
Use A \ Very \ Long \ Namespace as Ns;
-
// In this way, Ns can be used to replace the things defined in the ns of A/Very/Long/Namespace.
-
$ Foo = new Ns \ Foo ();
However, in some open-source projects, we often see usage such as use Ns \ Component, but it does not use as. This makes me have been thinking about whether there is a second usage of use, the worst thing is that there is no description of this usage in the php document. I can only guess it. I thought about this problem carefully later, A more reliable conclusion is that use can omit the as and the following alias and directly regard the name of the last node of ns as an alias. does it feel like the usage of the ln-s command:
[Php]View plain copy
-
// Usage 3
-
Require 'Inc. php ';
-
Use Foo \ Bar; // Bar is equal to Foo \ Bar.
-
$ Foo = new Bar ();