only the following types of code are affected by namespaces: classes (including abstracts and traits), interfaces, functions and constants.
Namespaces are declared using the namespace keyword. A file containing a namespace must declare the namespace at the top of the file before any other code – with one exception: the declare keyword.
Multiple namespaces may also be declared in the same file. it is recommended to use the alternate bracketed syntax.To combine global non-namespaced code with namespaced code, only bracketed syntax is supported. Global code should be encased in a namespace statement with no namespace name as in.
- Unqualified name, or an unprefixed class name like $a = new foo(); or foo::staticmethod();. If the current namespace is currentnamespace, this resolves to currentnamespacefoo. If the code is global, non-namespaced code, this resolves to foo. One caveat: unqualified names for functions and constants will resolve to global functions and constants if the namespaced function or constant is not defined. See Using namespaces: fallback to global function/constant for details.
- Qualified name, or a prefixed class name like $a = new subnamespacefoo(); orsubnamespacefoo::staticmethod();. If the current namespace is currentnamespace, this resolves tocurrentnamespacesubnamespacefoo. If the code is global, non-namespaced code, this resolves tosubnamespacefoo.
- Fully qualified name, or a prefixed name with global prefix operator like $a = new currentnamespacefoo(); orcurrentnamespacefoo::staticmethod();. This always resolves to the literal name specified in the code,currentnamespacefoo.
The value of
__NAMESPACE__ is a string that contains the current namespace name. In global, un-namespaced code, it contains an empty string.
All versions of PHP that support namespaces support three kinds of aliasing or importing: aliasing a class name, aliasing an interface name, and aliasing a namespace name. PHP 5.6+ also allows aliasing or importing function and constant names.
In PHP, aliasing is accomplished with the use operator.