PHP namespace parsing rules

Source: Internet
Author: User
: This article mainly introduces the PHP namespace parsing rules. if you are interested in the PHP Tutorial, refer to it. PHP namespace parsing rules

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, for example, Foo \ Bar.

Fully qualified name

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

Name resolution follows the following rules

  1. Calls to fully qualified functions, classes, and constants are parsed during compilation. For example, new \ A \ B is parsed as Class A \ B.
  2. 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 ().
  3. 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 ().
  4. 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 ().
  5. 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
    2. Try to find and call the function foo () in the global space ().
  6. 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:

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

    2. Try to automatically load class A \ B \ C.

    New D \ E () parsing:

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

    2. 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 ().

Name resolution example


  Static methodOr the namespace function B \ foo (); // call the namespace "A \ B" function "foo" B: foo (); // call the "foo" method of class "B" defined in namespace "A" // if the class "A \ B" is not found ", then try to automatically load the class "A \ B" D: foo (); // use the import rule, call the "foo" method of class "D" defined in namespace "B" // if class "B \ D" is not found, then try to automatically load the class "B \ D" \ B \ foo (); // call the function "foo" \ B: foo () in namespace "B (); // call the "foo" method of class "B" in the global space // if class "B" cannot be found, try to automatically loadStatic methodOr function A \ B: foo (); // call the "foo" method of class "B" defined in namespace "A \ A" // if class "A \ B" is not found, then try to automatically load the class "A \ B" \ A \ B: foo (); // call the "foo" method of class "B" defined in namespace "A \ B" // if class "A \ B" is not found, then try to automatically load the class "A \ B"?>

'). AddClass ('pre-numbering '). hide (); $ (this ). addClass ('Has-numbering '). parent (). append ($ numbering); for (I = 1; I <= lines; I ++) {$ numbering. append ($ ('
  • '). Text (I) ;}; $ numbering. fadeIn (1700) ;}) ;}; script

    The above describes the PHP namespace parsing rules, including static methods. I hope my friends who are interested in PHP tutorials will be helpful.

    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.