Home Backend Development PHP Tutorial PHP namespace resolution rules

PHP namespace resolution rules

Jul 29, 2016 am 09:12 AM
namespace new

PHP namespace resolution rules

Namespace name definition

Unqualified name

<code>名称中不包含命名空间分隔符的标识符,例如Foo
</code>
Copy after login

Qualified nameQualified name

<code>名称中含有命名空间分隔符的标识符,例如:Foo\Bar
</code>
Copy after login

Fully qualified name

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

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

<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>
Copy after login
').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 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Namespace merging in PHP8.0 Namespace merging in PHP8.0 May 14, 2023 am 08:43 AM

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

How to remove stickynotesnamespace How to remove stickynotesnamespace Dec 27, 2023 pm 01:51 PM

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.

What is the difference between make and new in go language What is the difference between make and new in go language Jan 09, 2023 am 11:44 AM

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.

How to use the new keyword in java How to use the new keyword in java May 03, 2023 pm 10:16 PM

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 work in js? How does the new operator work in js? Feb 19, 2024 am 11:17 AM

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? How to use PHP7's NameSpace and Use keywords to organize the structure of the code? Oct 19, 2023 am 08:07 AM

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

New Fujifilm fixed-lens GFX camera to debut new medium format sensor, could kick off all-new series New Fujifilm fixed-lens GFX camera to debut new medium format sensor, could kick off all-new series Sep 27, 2024 am 06:03 AM

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

See all articles