Home Backend Development PHP Tutorial PHP namespace: detailed explanation of subspace and public space instances

PHP namespace: detailed explanation of subspace and public space instances

May 18, 2017 pm 03:52 PM

The structure of a namespace is very similar to that of a file system. Folders can have subfolders, and namespaces can also define subspaces to describe the ownership relationships between each space. The previous chapter introduced What is a namespace? And the basic application of php namespace, here we start to understand the subspace and public space of the namespace.

Use the following example to illustrate what subspace is

For example, the two modules cart and order are both in the same shop project , expressing relationships through namespace subspaces.

The code is as follows:

<?php
namespace shop\cart;   //命名空间表示处于 shop 项目下的 cart
class Test {
}
namespace shop\order;  //命名空间表示处于 shop 项目下的 order
class Test {
   //申明与上面空间相同的类
}
$test =new Test();        //调用当前的类
$cart = new \shop\cart\Test();   //调用shop\cart空间的类
?>
Copy after login

The subspace of the namespace can also define many levels, such as cn\my\www\shop. Multi-level subspace claims are often made using the company domain name inverted, followed by the project name. The advantage of this is that the domain name is not repeated on the Internet, there will be no namespaces with the same name on the Internet, and you can also identify which company's specific project it is. It has a strong advertising effect.

Public space in the namespace

The public space in the namespace is easy to understand. In fact, there are no methods, class libraries and class libraries that define the namespace. Constants belong to the public space by default, which explains that most of the code written in previous versions can run in PHP5.3 and later versions. In addition, after the code segment in the public space is introduced into a certain namespace, the code segment in the public space does not belong to any namespace. For example, declare a script file common.php, and declare functions and classes in the file.

The code is as follows:

<?php
 function fun(){
   
 }
 class Demo{
   
 }
?>
Copy after login

Create another PHP file and introduce this script file common.php in a namespace, but it can be Classes and functions do not belong to this namespace. If no other namespaces are defined in this script, its elements are always in the public space.

The code is as follows:

<?php
 namespace cn\my;
 include &#39;common.php&#39;;  //引入当前目录下的文件
 $demo = new Demo;    //出现致命错误:找不到 cn\my\Demo类
 $demo = new \Demo(); //正确的方式  加上 \
 var_dump();   // 错误,系统函数在公共空间
 \var_dump();  // 正确,使用了 \
?>
Copy after login

Note: The way to call the public space is to add \ directly in front of the element name. Otherwise, the PHP parser will think that the user is calling the element under the current space. In addition to custom elements, there are also elements that come with PHP, which all belong to the public space. In fact, public space functions and constants can be called normally without adding \, but in order to correctly distinguish the area where the element is located, it is recommended to add \ when calling the function.

The subspaces and public spaces of the namespace are introduced here. The next section brings you the three types of names and terms in the .

【Recommended related tutorials】

1. "php.cn Dugu Jiujian (4)-php video tutorial"

2 . Video tutorial: Namespace: Although we have the same name and the same gender, we belong to different time and space

3. Full set of tutorials on PHP programming from entry to mastery

The above is the detailed content of PHP namespace: detailed explanation of subspace and public space instances. For more information, please follow other related articles on the PHP Chinese website!

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 Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Hot Topics

Java Tutorial
1667
14
PHP Tutorial
1273
29
C# Tutorial
1255
24
Solve PHP error: The specified namespace class was not found Solve PHP error: The specified namespace class was not found Aug 18, 2023 pm 11:28 PM

Solve PHP error: The specified namespace class was not found. When developing using PHP, we often encounter various error messages. One of the common errors is "The specified namespace class was not found". This error is usually caused by the imported class file not being properly namespace referenced. This article explains how to solve this problem and provides some code examples. First, let’s take a look at an example of a common error message: Fatalerror:UncaughtError:C

Design ideas and implementation methods of Redis namespace and expiration mechanism Design ideas and implementation methods of Redis namespace and expiration mechanism May 11, 2023 am 10:40 AM

Redis is an open source, high-performance key-value storage database. When using Redis for data storage, we need to consider the design of the key namespace and expiration mechanism to maintain Redis performance and data integrity. This article will introduce the design ideas and implementation methods of Redis' namespace and expiration mechanism. 1. Redis namespace design ideas In Redis, keys can be set arbitrarily. In order to facilitate the management and distinction of different data types, Redis introduces the concept of namespace. Life

How to use namespace in F3 framework? How to use namespace in F3 framework? Jun 03, 2023 am 08:02 AM

The F3 framework is a simple, easy-to-use, flexible and scalable PHPWeb framework. Its namespace (Namespace) mechanism provides us with a more standardized, more readable, and clearer code structure. In this article, we will explore how to use namespaces in the F3 framework. 1. What is a namespace? Namespaces are often used to solve the problem of naming conflicts in PHP. It can encapsulate one or more classes, functions or constants in a namespace, which is equivalent to adding a prefix to them. example

C++ syntax error: undefined namespace used, how to deal with it? C++ syntax error: undefined namespace used, how to deal with it? Aug 21, 2023 pm 09:49 PM

C++ is a widely used high-level programming language. It has high flexibility and scalability, but it also requires developers to strictly master its grammatical rules to avoid errors. One of the common errors is "use of undefined namespace". This article explains what this error means, why it occurs, and how to fix it. 1. What is the use of undefined namespace? In C++, namespaces are a way of organizing reusable code in order to keep it modular and readable. You can use namespaces to make functions with the same name

Example of new features in PHP8: How to use namespaces and codes to better organize the code structure? Example of new features in PHP8: How to use namespaces and codes to better organize the code structure? Sep 11, 2023 pm 12:22 PM

Example of new features in PHP8: How to use namespaces and codes to better organize the code structure? Introduction: PHP8 is an important version of the PHP programming language, which introduces many exciting new features and improvements. One of the most important new features is namespaces. Namespaces are a way to organize your code into a better structure that avoids conflicts between classes, functions, and constants with the same name. In this article, we’ll look at how to leverage namespaces and codes to better structure your PHP8 code

PHP extension development: How to use namespaces to organize and manage custom functions? PHP extension development: How to use namespaces to organize and manage custom functions? Jun 04, 2024 pm 12:59 PM

It is crucial to manage custom functions using namespaces, which allow developers to create their own naming ranges and prevent name conflicts. The steps include: creating a namespace, using the use statement to import the namespace, and calling the namespace function. In a practical case, the MyMath extension demonstrates how to use namespaces to organize mathematical functions to improve readability and maintainability.

How to use PHP7's namespace and automatic loading mechanism to organize the structure of the code? How to use PHP7's namespace and automatic loading mechanism to organize the structure of the code? Oct 20, 2023 am 08:57 AM

How to use PHP7's namespace and automatic loading mechanism to organize the structure of the code? Abstract: With the launch of PHP7, namespace and automatic loading mechanism have become important features that cannot be ignored in PHP development. This article will introduce how to use PHP7's namespace and automatic loading mechanism to organize the structure of the code, and illustrate it through specific code examples. 1. What is a namespace? Namespace is a mechanism introduced in PHP7 to resolve naming conflicts that may occur between different class libraries or code files. via namespace

PHP 5.3 new feature: How to use namespaces to resolve class name conflicts PHP 5.3 new feature: How to use namespaces to resolve class name conflicts Jul 30, 2023 pm 12:25 PM

New features of PHP5.3: How to use namespaces to solve class name conflicts Introduction: During the development of PHP, as projects become larger and more complex, class name conflicts also arise. In order to solve this problem, PHP5.3 version introduced the concept of namespace. Namespaces provide a way to organize related classes, functions, and constants together to avoid naming conflicts. This article will introduce in detail the concept of PHP namespaces and how to use namespaces to solve class name conflicts, with code examples.

See all articles