PHP namespace: detailed explanation of subspace and public space instances

伊谢尔伦
Release: 2023-03-07 19:26:02
Original
2668 people have browsed it

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 introducedWhat 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:

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:

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:

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!

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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!