Note 009 PHP namespaces-Post

Source: Internet
Author: User

Name resolution rules

Before we describe the name resolution rules, we'll look at some important definitions:

namespace name definition

Unqualified names unqualified Name

Identifiers that do not contain namespace separators in the name, such as Foo

Qualified name qualified name

The name contains the identifier for the namespace delimiter, such as Foo\bar

Fully qualified name fully qualified name

The 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.

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: 1. Look for function 2 named A\b\foo () in the current namespace. Try to find and invoke the 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. Here is the parsing process for calling new C () and New D\e ():

Resolution of New C ():

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

Attempt to automatically load class a\b\c.

Parsing of New D\e ():

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

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

<?phpnamespace A;use b\d, c\e as f;//function call Foo ();     First try calling the function foo () defined in namespace "A"//and then try calling the global function "foo" \foo ();   Call Global space function "foo" My\foo ();        The call is defined in the namespace "a\my" in the function "foo" F ();    First try calling the function "F" defined in Namespace "a"//and then try calling global function "F"//class referencing New B ();    Create an object of class ' B ' defined in namespace ' A '///If not found, attempt to automatically load class "a\b" New D ();    Use an import rule to create an object of class ' D ' defined in namespace ' B '//If not found, attempt to automatically load class "b\d" New F ();   Use an import rule to create an object of class "E" defined in the namespace "C"//If not found, try to automatically load class "C\e" new \b ();   Create an object that defines the class "B" in the global space//If it is not found, try to automatically load class "B" new \d ();   Create an object that defines the class "D" in the global space//If it is not found, try to automatically load class "D" new \f ();    Create an object that defines the class "F" in the global space//If it is not found, try to automatically load the class "F"//Call the static method in another namespace or the 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, attempt to automatically load static method or function A\b::foo () in class "B"//current namespace; Call the class "B" defined in the namespace "A\a". "Foo" method//If the class "a\a\b" is not found, try to automatically load 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"?>

The above is the note 009 PHP namespace-After the content, more relevant content please pay attention to topic.alibabacloud.com (www.php.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.