Note 009PHP namespace-later

Source: Internet
Author: User
Before describing the name resolution rules, let's take a look at some important definitions:

Name resolution rules

Before describing the name resolution rules, let's take a look at some important definitions:

Namespace name definition

Unqualified name

The name does not contain the namespace separator identifier, such as Foo.

Qualified name

The name contains the namespace separator identifier, such as Foo \ Bar

Fully qualified name

The name contains the namespace separator and the identifier starting with the namespace separator, for example, \ Foo \ Bar. Namespace \ Foo is also a fully qualified name.

Name resolution follows the following rules:

Calls to fully qualified functions, classes, and constants are parsed during compilation. For example, new \ A \ B is parsed as Class A \ B.

All unqualified names and qualified names (not fully qualified names) are converted during compilation according to the current import rules. For example, if the namespace A \ B \ C is imported as C () is converted to A \ B \ C \ D \ e ().

Within the namespace, all qualified names that are not converted according to the import rules will be prefixed with the current namespace name. For example, if C \ D \ e () is called in namespace A \ B, C \ D \ e () is converted to A \ B \ C \ D \ e ().

The unqualified class name is converted during compilation according to the current import rule (the short import name is replaced by the full name ). For example, if the namespace A \ B \ C is imported as C, new C () is converted to new A \ B \ C ().

In A namespace (for example, A \ B), function calls with non-qualified names are parsed at runtime. For example, the call to function foo () is parsed as follows: 1. find the function named A \ B \ foo () in the current namespace. try to find and call the function foo () in the global space ().

Calls to A non-qualified name or qualified name class (not fully qualified name) within A namespace (for example, A \ B) are resolved at runtime. The following is the parsing process of calling new C () and new D \ E:

New C () parsing:

Find the \ B \ C class in the current namespace.

Try to automatically load class A \ B \ C.

New D \ E () parsing:

Add the name of the namespace before the class name to A \ B \ D \ E, and then search for the class.

Try to automatically load class A \ B \ D \ E.

To reference global classes in a global namespace, you must use the fully qualified name new \ C ().

 

The above is the namespace of note PHP-the content in the later part. For more information, see PHP Chinese website (www.php1.cn )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.