Home  >  Article  >  Backend Development  >  PHP namespace resolution rules

PHP namespace resolution rules

WBOY
WBOYOriginal
2016-07-29 09:12:321132browse

PHP namespace resolution rules

Namespace name definition

Unqualified name

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

Qualified nameQualified name

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

Fully qualified name

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

Name resolution follows the following rules

  1. For fully qualified Names of functions, classes and constants in calls are resolved at compile time. For example new AB resolves to class AB.
  2. All unqualified names and qualified names (non-fully qualified names) are converted at compile time according to the current import rules. For example, if namespace ABC is imported as C, then calls to CDe() are converted to ABCDe().
  3. Within the namespace, all qualified names that are not converted according to the import rules will be preceded by the current namespace name. For example, if CDe() is called within namespace AB, CDe() will be converted to ABCDe().
  4. Unqualified class names are converted at compile time according to the current import rules (full names are used instead of short import names). For example, if namespace ABC is imported as C, then new C() is converted to new ABC().
  5. Within a namespace (e.g. AB), function calls to unqualified names are resolved at runtime. For example, a call to function foo() is parsed like this:
    1. Find a function named ABfoo() in the current namespace
    2. Try to find and call function foo() in the global space.
  6. Calls to unqualified names or qualified name classes (non-fully qualified names) inside a namespace (e.g. AB) are resolved at runtime. The following is the parsing process of calling new C() and new DE():

    Parsing of new C():

    1. Find class ABC in the current namespace;

    2. Try to automatically load class ABC.

    new DE() analysis:

    1. Add the current namespace name in front of the class name to become: ABDE, and then find the class

    2. Try to automatically load the class ABDE.

    In order to refer to a global class in the global namespace, the fully qualified name new C() must be used.

Name resolution example

静态方法或命名空间函数

B\foo();    // 调用命名空间 "A\B" 中函数 "foo"

B::foo();   // 调用命名空间 "A" 中定义的类 "B" 的 "foo" 方法
            // 如果未找到类 "A\B" ,则尝试自动装载类 "A\B"

D::foo();   // 使用导入规则,调用命名空间 "B" 中定义的类 "D" 的 "foo" 方法
            // 如果类 "B\D" 未找到,则尝试自动装载类 "B\D"

\B\foo();   // 调用命名空间 "B" 中的函数 "foo" 

\B::foo();  // 调用全局空间中的类 "B" 的 "foo" 方法
            // 如果类 "B" 未找到,则尝试自动装载类 "B"

// 当前命名空间中的静态方法或函数

A\B::foo();   // 调用命名空间 "A\A" 中定义的类 "B" 的 "foo" 方法
              // 如果类 "A\A\B" 未找到,则尝试自动装载类 "A\A\B"

\A\B::foo();  // 调用命名空间 "A\B" 中定义的类 "B" 的 "foo" 方法
              // 如果类 "A\B" 未找到,则尝试自动装载类 "A\B"
?>
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn