Php.net
(PHP 5 >= 5.3.0, PHP 7)
Define namespaces
Although any valid PHP code can be included in a namespace, only the following types of code are affected by namespaces: classes (including abstract classes and traits), interfaces, functions, and constants.
If a file contains a namespace, it must declare the namespace before all other code, except one: the DECLARE keyword. All non-PHP code, including whitespace characters, cannot appear before the declaration of the namespace.
The same namespace can be defined in multiple files, which allows the content of the same namespace to be split into separate files.
Defining child namespaces
Define multiple namespaces in the same file
Note: In practical programming practice, it is not recommended to define multiple namespaces in the same file!
Using namespaces
Three ways to refer to the class name:
Unqualified names (identifiers that do not contain namespace separators in the name), or class names that do not contain prefixes. For example $a =new foo (); or Foo::staticmethod ();. If the current namespace is Currentnamespace,foo, it will be resolved to Currentnamespace\foo. If the code that uses Foo is global and does not contain code in any namespace, Foo is parsed as Foo. * * WARNING * *: If a function or constant in a namespace is undefined, the unqualified function name or constant name is resolved to the global function name or constant name.
The qualified name (the identifier with the namespace delimiter in the name), or the name that contains the prefix, for example $a = new Subnamespace\foo (); or Subnamespace\foo::staticmethod ();. If the current namespace is Currentnamespace, Foo will be parsed to Currentnamespace\subnamespace\foo. If the code that uses Foo is global and does not contain code in any namespace, Foo is parsed as Subnamespace\foo.
The fully qualified name (the identifier that contains the namespace delimiter in the name and begins with the namespace delimiter), or contains the name of the global prefix operator. To access any global class, function, or constant, you can use a fully qualified name, such as \strlen () or \exception.
Tips
$a = ' \namespacename\classname '; $obj = new $a; $a = ' namespacename\classname '; $obj = new $a; $b = ' namespacename\funcname '; $b (); Prints namespacename\funcname$b = ' \namespacename\funcname '; $b (); Also prints Namespacename\funcname
Working with related constants
The value of the constant __namespace__ is a string containing the name of the current namespace. In the global, not included in any namespace code, it contains an empty string.
Using namespaces: Aliases/imports
The class name always resolves to the name in the current namespace. Therefore, you must use a fully qualified name when accessing the name of a class that is inside the system or that is not included in the namespace.