Home > Backend Development > PHP Tutorial > Detailed explanation of namespace in PHP

Detailed explanation of namespace in PHP

不言
Release: 2023-04-03 15:08:02
Original
1408 people have browsed it

This article introduces to you a detailed explanation of the namespace in PHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

PHP’s namespace (namespace) is only available after php5.3. This concept has existed in C# for a long time. The namespace in PHP is actually the same concept as C#.

Why do we need to use namespace in php?

Assuming that if namespace is not used, the name of each class in a project must be fixed. Because when PHP is new, whether it calls autoload or calls a loaded class, there is a file corresponding to the class name. So when there is no namespace, we will think of various naming rules to distinguish different classes, such as project1_school1_class1_Student or project2_school_class_Student.

After introducing namespace, this can be effectively circumvented. A namespace is equivalent to corresponding to a file path. When searching for this class, the class definition file will be found in the corresponding file path.

Definition and use of namespace

Definition:

<code class="language-php"><?php<br/><br/>namespace Myproject;</code>
Copy after login

or

<code class="language-php"><?php<br/><br/>namespace Myproject {<br/><br/>}</code>
Copy after login

Use:

<code class="language-php"><?php<br/><br/>use Myproject/School;</code>
Copy after login
<code class="language-php"><?php<br/><br/>use Myproject/School as School1;   // 别名</code>
Copy after login

Naming Spaces are resolved at runtime. use is equivalent to a statement and is not parsed or loaded. For example, the following example:

test.php

<code class="language-php"><?php<br/>use my\name;<br/>require_once("/home/yejianfeng/handcode/test/namespace1.php");<br/>$a = new my\name\A();<br/>$a->Print1();</code>
Copy after login

namespace1.php

<code class="language-php"><?php<br/>namespace my\name;<br/>class A {<br/>        public function Print1(){<br/>                echo 11;<br/>        }<br/>}</code>
Copy after login

Although require_once is under use, it can still run normally, because the program can only be run under new my\ Name\A() is used to load the namespace my\name

global class and namespace class

If you want to create a new global class, use new \A( )

If you want to create a new namespace class, use new my\namespace\A()

The order of the namespace

Since there is a name After the space, the most error-prone thing is when using a class, what is the search path for this class.

If you can figure out this example in the manual, you can figure out the search order.

<code class="language-php"><?php<br/>namespace A;<br/>use B\D, C\E as F;<br/><br/>// 函数调用<br/><br/>foo();      // 首先尝试调用定义在命名空间"A"中的函数foo()<br/>            // 再尝试调用全局函数 "foo"<br/><br/>\foo();     // 调用全局空间函数 "foo" <br/><br/>my\foo();   // 调用定义在命名空间"A\my"中函数 "foo" <br/><br/>F();        // 首先尝试调用定义在命名空间"A"中的函数 "F" <br/>            // 再尝试调用全局函数 "F"<br/><br/>// 类引用<br/><br/>new B();    // 创建命名空间 "A" 中定义的类 "B" 的一个对象<br/>            // 如果未找到,则尝试自动装载类 "A\B"<br/><br/>new D();    // 使用导入规则,创建命名空间 "B" 中定义的类 "D" 的一个对象<br/>            // 如果未找到,则尝试自动装载类 "B\D"<br/><br/>new F();    // 使用导入规则,创建命名空间 "C" 中定义的类 "E" 的一个对象<br/>            // 如果未找到,则尝试自动装载类 "C\E"<br/><br/>new \B();   // 创建定义在全局空间中的类 "B" 的一个对象<br/>            // 如果未发现,则尝试自动装载类 "B"<br/><br/>new \D();   // 创建定义在全局空间中的类 "D" 的一个对象<br/>            // 如果未发现,则尝试自动装载类 "D"<br/><br/>new \F();   // 创建定义在全局空间中的类 "F" 的一个对象<br/>            // 如果未发现,则尝试自动装载类 "F"<br/><br/>// 调用另一个命名空间中的静态方法或命名空间函数<br/><br/>B\foo();    // 调用命名空间 "A\B" 中函数 "foo"<br/><br/>B::foo();   // 调用命名空间 "A" 中定义的类 "B" 的 "foo" 方法<br/>            // 如果未找到类 "A\B" ,则尝试自动装载类 "A\B"<br/><br/>D::foo();   // 使用导入规则,调用命名空间 "B" 中定义的类 "D" 的 "foo" 方法<br/>            // 如果类 "B\D" 未找到,则尝试自动装载类 "B\D"<br/><br/>\B\foo();   // 调用命名空间 "B" 中的函数 "foo" <br/><br/>\B::foo();  // 调用全局空间中的类 "B" 的 "foo" 方法<br/>            // 如果类 "B" 未找到,则尝试自动装载类 "B"<br/><br/>// 当前命名空间中的静态方法或函数<br/><br/>A\B::foo();   // 调用命名空间 "A\A" 中定义的类 "B" 的 "foo" 方法<br/>              // 如果类 "A\A\B" 未找到,则尝试自动装载类 "A\A\B"<br/><br/>\A\B::foo();  // 调用命名空间 "A\B" 中定义的类 "B" 的 "foo" 方法<br/>              // 如果类 "A\B" 未找到,则尝试自动装载类 "A\B"<br/>?></code>
Copy after login

Recommended related articles:

Naming rules for php variables and usage of php variables (with code)

How to use php 32 Encryption and decryption of id based on hexadecimal system (code attached)

The above is the detailed content of Detailed explanation of namespace in PHP. For more information, please follow other related articles on the PHP Chinese website!

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