PHP namespace parsing rules

Source: Internet
Author: User
PHP namespace parsing rules

namespace name definition

Unqualified names unqualified Name

名称中不包含命名空间分隔符的标识符,例如Foo

Qualified name qualified name

名称中含有命名空间分隔符的标识符,例如:Foo\Bar

Fully qualified name fully qualified name

名称中包含命名空间分隔符,并以命名空间分隔符开始的标识符,例如:\Foo\Bar.namespace\Foo 也是一个完全限定名称。

Name resolution follows these rules

    1. 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.
    2. 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 ().
    3. 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 ().
    4. 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 ().
    5. 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:
      1. Look for a function named A\b\foo () in the current namespace
      2. Try to find and invoke the function foo () in global space.
    6. Calls to unqualified or qualified name classes (not fully qualified names) within a namespace (for example, a\b) are resolved at run time. Here is the parsing process for calling new C () and New D\e ():

      Resolution of New C ():

      1. Finds the A\b\c class in the current namespace;

      2. Attempt to automatically load class a\b\c.

      Parsing of New D\e ():

      1. Precede the class name with the current namespace name into: A\b\d\e, and then look for the class

      2. Attempt to automatically load class a\b\d\e.

      In order to refer to global classes in the global namespace, you must use the fully qualified name new \c ().

Name resolution Example


  Static methodsor namespace function B\foo ();   Call namespace "a\b" in function "foo" B::foo ();   Call the "Foo" Method of Class "B" defined in the namespace "A"//If the class "a\b" is not found, try to automatically load the class "a\b" D::foo ();   Using the import rule, call the "Foo" Method of Class "D" defined in the namespace "B"///If the class "b\d" is not found, try to automatically load class "B\d" \b\foo ();  Call the function "foo" in the Namespace "B" \b::foo (); Call the "Foo" Method of Class "B" in global space//If class "B" is not found, try to automatically load the class "B"//////in the current namespaceStatic Methodsor function A\b::foo ();  Call the "Foo" Method of Class "B" defined in the namespace "a\a"///If the class "a\a\b" is not found, try to automatically load the class "a\a\b" \a\b::foo (); Call the "Foo" Method of Class "B" defined in the namespace "a\b"///If the class "a\b" is not found, 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); }); });

    The above describes the PHP namespace parsing rules, including the static method aspects of the content, I hope that the PHP tutorial interested in a friend 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.