Namespace Overview
In PHP, namespaces are used to solve two types of problems encountered when creating reusable code such as classes or functions when writing class libraries or applications:
Name conflicts between user-written code and PHP internal classes/functions/constants or third-party classes/functions/constants.
Create an alias (or short) name for a very long identifier name (usually defined to alleviate the first type of problem), improving the readability of the source code.
PHP namespaces provide a way to group related classes, functions, and constants together. Here is an example illustrating PHP namespace syntax:
Define namespace
Although any legal PHP code can be included in a namespace, there are only three types of code affected by namespaces: classes, functions, and constants. Namespaces are declared using the keyword namespace. If a file contains a namespace, it must declare the namespace before all other code. In addition, unlike other language features of PHP, the same namespace can be defined in multiple files, which allows the contents of the same namespace to be divided and stored in different files. Of course you can also define multiple namespaces in the same file.
Define sub-namespaces: Much like the relationship between directories and files, PHP namespaces also allow you to specify hierarchical namespace names. Therefore, namespace names can be defined in a hierarchical manner:
Defining multiple namespaces in the same file: There are two ways to declare multiple namespaces in the same file. However, in actual programming practice, it is highly discouraged to define the Dogo namespace in the same file. This method is mainly used to combine multiple PHP scripts in the same file. The first method is listed below.
However, it is strongly not recommended to use this method. You can refer to the following brace definition method:
Elements in the PHP namespace use
Before discussing how to use namespaces, you must understand how PHP knows which namespace elements to use. Class names can be referenced in three ways:
Unqualified name, or a class name without a prefix, such as $a=new foo(); or foo::staticmethod();. If the current namespace is currentnamespace, foo will be resolved to currentnamespacefoo. If the code using foo is global and does not contain code in any namespace, foo will be resolved as foo. Warning: If a function or constant in the namespace is undefined, the unqualified function or constant name is resolved to a global function or constant name. See Using Namespaces: Fallback Global Function Names/Constant Names for details.
Qualified name, or name including a prefix, such as $a = new subnamespacefoo(); or subnamespacefoo::staticmethod();. If the current namespace is currentnamespace, foo will be resolved to currentnamespacesubnamespacefoo. If the code using foo is global, code not contained in any namespace, foo will be resolved to subnamespacefoo.
Fully qualified name, or a name that includes a global prefix operator, for example, $a = new currentnamespacefoo(); or currentnamespacefoo::staticmethod();. In this case, foo is always resolved to the literal name currentnamespacefoo in the code.
Use namespace: alias/import
Allowing external fully qualified names to be referenced or imported via aliases is an important feature of namespaces. PHP namespace support There are two ways to use aliases or imports: using aliases for class names, or using aliases for namespace names. In PHP, aliases are implemented through the operator use.
Note that PHP does not support imported functions or constants.
// The following example is the same as use MyFullNSname as NSname
use MyFullNSname;
//Import a global class
use ArrayObject;
Name resolution rules
Before explaining the name resolution rules, let’s look at some important definitions:
Unqualified name: An identifier that does not contain a namespace delimiter in its name, such as Foo
Qualified name: An identifier that contains a namespace delimiter in its name, such as FooBar
Fully qualified name Fully qualified name: An identifier whose name contains a namespace delimiter and starts with a namespace delimiter, such as FooBar. namespaceFoo is also a fully qualified name.
Name resolution follows the following rules:
Calls to fully qualified names of functions, classes and constants are resolved at compile time. For example new AB resolves to class AB.
All unqualified names and qualified names (not fully qualified names) are converted at compile time according to the current import rules. For example, if namespace ABC is imported as C, then calls to CDe() are converted to ABCDe().
Within a namespace, all qualified names that are not converted according to import rules will have the current namespace name prepended to them. For example, if CDe() is called inside namespace AB, CDe() will be converted to ABCDe().
Unqualified class names are converted at compile time according to the current import rules (full names are used instead of short import names). For example, if namespace ABC is imported as C, new C() is converted to new ABC() .
Within a namespace (e.g. AB), function calls to unqualified names are resolved at runtime. For example, a call to function foo() is parsed as follows:
1) Find a function named ABfoo() in the current namespace
2) Try to find and call the function foo() in the global space ).
Calls to unqualified names or qualified name classes (non-fully qualified names) inside a namespace (e.g. AB) are resolved at runtime. The following is the parsing process of calling new C() and new DE(): Parsing of new C():
Find the ABC class in the current namespace.
Try to autoload class ABC.
Parsing of new DE():
Add the current namespace name in front of the class name to become: ABDE, and then search for the class.
Try autoloading class ABDE.
In order to reference a global class in the global namespace, the fully qualified name new C() must be used.
Example name resolution example
http://www.bkjia.com/PHPjc/788610.html