Home > Backend Development > PHP Tutorial > Note 009 PHP namespace - Part 2

Note 009 PHP namespace - Part 2

黄舟
Release: 2023-03-04 09:04:02
Original
1120 people have browsed it

Name resolution rules

Before explaining the name resolution rules, let's look at some important definitions:

Namespace name definition

Unqualified nameUnqualified name

Identifiers whose names do not contain namespace separators, such as Foo

Qualified name Qualified name

Identifiers whose names contain namespace separators, such as FooBar

fully Qualified name Fully qualified name

An identifier whose name contains a namespace delimiter and starts with a namespace delimiter, such as FooBar. namespaceFoo is also a fully qualified name.

Name resolution follows the following rules:

Calls to functions, classes, and constants with fully qualified names are resolved at compile time. For example new AB resolves to class AB.

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

Within a namespace, all qualified names that are not converted according to import rules will have the current namespace name prepended to them. For example, if CDe() is called inside namespace AB, CDe() will be converted to ABCDe().

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, new C() is converted to new ABC() .

Within a namespace (e.g. AB), function calls to unqualified names are resolved at runtime. For example, a call to function foo() is parsed as follows: 1. Find a function named ABfoo() in the current namespace 2. Try to find and call function foo() in the global space.

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

Find the ABC class in the current namespace.

Try to autoload class ABC.

Parsing of new DE():

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

Try to autoload class ABDE.

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

<?php
namespace A;
use B\D, C\E as F;// 函数调用

foo();      // 首先尝试调用定义在命名空间"A"中的函数foo()
// 再尝试调用全局函数 "foo"
\foo();     // 调用全局空间函数 "foo" 
my\foo();   // 调用定义在命名空间"A\my"中函数 "foo" 
F();        // 首先尝试调用定义在命名空间"A"中的函数 "F" 
// 再尝试调用全局函数 "F"
// 类引用
new B();    // 创建命名空间 "A" 中定义的类 "B" 的一个对象
// 如果未找到,则尝试自动装载类 "A\B"
new D();    // 使用导入规则,创建命名空间 "B" 中定义的类 "D" 的一个对象
// 如果未找到,则尝试自动装载类 "B\D"
new F();    // 使用导入规则,创建命名空间 "C" 中定义的类 "E" 的一个对象
// 如果未找到,则尝试自动装载类 "C\E"
new \B();   // 创建定义在全局空间中的类 "B" 的一个对象
// 如果未发现,则尝试自动装载类 "B"
new \D();   // 创建定义在全局空间中的类 "D" 的一个对象
// 如果未发现,则尝试自动装载类 "D"
new \F();   // 创建定义在全局空间中的类 "F" 的一个对象
// 如果未发现,则尝试自动装载类 "F"
// 调用另一个命名空间中的静态方法或命名空间函数

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"
?>
Copy after login

The above is the content of note 009 PHP namespace - the second part. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template