(PHP 5 >= 5.3.0, PHP 7)
in the PHP namespace, the class name can be referenced in three ways:
- Unqualified name (Unqualified name), which does not contain an identifier for the namespace delimiter, such as Foo.
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.
- < Strong> qualified name ( qualified name) with the identifier for the namespace delimiter, such as Foo\bar.
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.
- fully qualified name, The (Fully qualified name) name contains the namespace delimiter, and an identifier that starts with a namespace delimiter, such as \foo\bar. Namespace\foo is also a fully qualified name.
$a new \currentnamespace\foo (); or \currentnamespace\foo::staticmethod ();. In this case, Foo is always parsed into the literal name (literal name) Currentnamespace\foo in the code.
name resolution follows these rules:
Calls to fully qualified names for functions, classes, and constants are parsed at compile time. For example, new \a\b resolves to class a\b.
All unqualified and qualified names (not fully qualified names) are converted at compile time according to the current import rules. For example, if the namespace a\b\c is imported as C, then the call to c\d\e () is converted to a\b\c\d\e ().
Inside the namespace, all qualified names that are not translated according to the import rule are preceded by the current namespace name. For example, if you call c\d\e ()inside the namespace a\b , c\d\e ( ) is converted to a\b\c\d\e () .
Unqualified class names are converted at compile time based on the current import rule (instead of the short import name with the full name). For example, if the namespace a\b\c is imported as C, then new C () is converted to new a\b\c () .
Within a namespace (for example, a\b), a function call to an unqualified name is parsed at run time. For example, the call to function foo () is parsed like this:
Looks for a function named A\b\foo () in the current namespace to attempt to find and Invoke function foo () in global space.
Calls to unqualified or qualified name classes (not fully qualified names) within a namespace (for example, a\b) are resolved at run time. The following is the parsing process for calling new C () :
Looks for a function named A\b\foo () in the current namespace to attempt to find and Invoke function foo () in global space.
7. in order to refer to global classes in the global namespace, you must use the fully qualified name new \c ().
Namespaces-Name resolution rules