PHP namespace resolution rules
PHP namespace resolution rules
Namespace name definition
Unqualified name
<code>名称中不包含命名空间分隔符的标识符,例如Foo </code>
Qualified nameQualified name
<code>名称中含有命名空间分隔符的标识符,例如:Foo\Bar </code>
Fully qualified name
<code>名称中包含命名空间分隔符,并以命名空间分隔符开始的标识符,例如:\Foo\Bar. namespace\Foo 也是一个完全限定名称。 </code>
Name resolution follows the following rules
- For fully qualified Names of functions, classes and constants in calls 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 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().
- 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().
- 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:
- Find a function named ABfoo() in the current namespace
- 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 class ABC in the current namespace;
Try to automatically load class ABC.
new DE() analysis:
Add the current namespace name in front of the class name to become: ABDE, and then find the class
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
<code><?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" // 调用另一个命名空间中的<strong>静态方法</strong>或命名空间函数 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" // 当前命名空间中的<strong>静态方法</strong>或函数 A\B::foo(); // 调用命名空间 "A\A" 中定义的类 "B" 的 "foo" 方法 // 如果类 "A\A\B" 未找到,则尝试自动装载类 "A\A\B" \A\B::foo(); // 调用命名空间 "A\B" 中定义的类 "B" 的 "foo" 方法 // 如果类 "A\B" 未找到,则尝试自动装载类 "A\B" ?> </code>
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.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



With the continuous development of technology, the PHP language is constantly being updated and improved. PHP8.0, as the latest version of the PHP language, is more powerful than previous versions. One of its important features is namespace merging. So, what is namespace merging? What's the use of it? Let’s discuss this in detail below. Namespace is a namespace in PHP used to distinguish different codes. It allows us to use classes, functions or constants with the same name in the code. in previous versions

When we use win7 system, we will find a stickynotes tool. Stickynotes actually functions like an electronic notepad. If you want to delete words, you need to uninstall it in the control panel. What is stickynotes namespace and how to delete it? Method 1: Find - Method 2: Find it and click delete later. Method 3: Use third-party software to crush and delete. What is StickyNotes? It is a sticky note tool that comes with the system, similar to sticky notes. Floating on the desktop, it is convenient for users to write down important things. Usually the software is in the hidden state. If necessary, it can jump out at a designated time and remind users.

Differences: 1. Make can only be used to allocate and initialize data of types slice, map, and chan; while new can allocate any type of data. 2. New allocation returns a pointer, which is the type "*Type"; while make returns a reference, which is Type. 3. The space allocated by new will be cleared; after make allocates the space, it will be initialized.

1. Concept In the Java language, the "new" expression is responsible for creating an instance, in which the constructor is called to initialize the instance; the return value type of the constructor itself is void, not "the constructor returns the newly created Object reference", but the value of the new expression is a reference to the newly created object. 2. Purpose: Create an object of a new class. 3. Working mechanism: Allocate memory space for object members, and specify default values. Explicitly initialize member variables, perform construction method calculations, and return reference values. 4. Instance new operation often means opening up new memory in the memory. The memory space is allocated in the heap area in the memory. It is controlled by jvm and automatically manages the memory. Here we use the String class as an example. Pu


How does the new operator in js work? Specific code examples are needed. The new operator in js is a keyword used to create objects. Its function is to create a new instance object based on the specified constructor and return a reference to the object. When using the new operator, the following steps are actually performed: create a new empty object; point the prototype of the empty object to the prototype object of the constructor; assign the scope of the constructor to the new object (so this points to new object); execute the code in the constructor and give the new object

How to use PHP7's NameSpace and Use keywords to organize the structure of the code? Introduction: In software development, the organizational structure of the code is very important. It is directly related to the readability, maintainability and scalability of the code. With the continuous iteration of PHP versions, PHP7 introduced NameSpace and Use keywords, which provide us with more flexibility and convenience. This article will introduce how to use PHP7's NameSpace and Use keywords to organize the structure of the code, and provide specific code

Fujifilm has seen a lot of success in recent years, largely due to its film simulations and the popularity of its compact rangefinger-style cameras on social media. However, it doesn't seem to be resting on its laurels, according to Fujirumors. The u
