Home > Backend Development > PHP Tutorial > Detailed explanation of the role of php namespace

Detailed explanation of the role of php namespace

伊谢尔伦
Release: 2023-03-07 20:10:01
Original
4124 people have browsed it

One of the clearest functions of the php namespace is to solve the problem of duplicate names. In PHP, two functions or classes are not allowed to have the same name, otherwise a fatal error will occur. The previous chapter introduced What is the PHP namespace. The PHP official website has clearly defined and explained it visually. Here is a paragraph copied directly from the PHP official website: In a broad sense, namespace is a method of encapsulating things. This abstract concept can be found in many places. For example, directories are used in operating systems to group related files, and they act as namespaces for the files in the directory. For example, the file foo.txt can exist in the directories /home/greg and /home/other at the same time, but two foo.txt files cannot exist in the same directory. Additionally, when accessing the foo.txt file outside the directory /home/greg, we must put the directory name and directory separator before the file name to get /home/greg/foo.txt. The application of this principle to the field of programming is the concept of namespace.

The introduction of the php namespacenamespace keyword is to solve various "trouble" that have occurred in the php object-oriented programming process; the specific troubles are as follows:

  1. Name conflicts between user-written code and PHP internal classes/functions/constants or third-party classes/functions/constants.

  2. In order to alleviate trouble 1, when writing various classes, longer class names are usually used or name prefixes (or suffixes) are added to classes that implement different functions.

  3. When the magic function __autoload is not used, and each class occupies an exclusive PHP file, in order to call different classes, other PHP files using these classes will be used. Write more include (or require or require_once) statements at the beginning.

We first create a namespace. Multiple namespaces can be created in the same script file.

The code is as follows:

<?php
//创建一个名为&#39;Article&#39;的命名空间
namespace Article;
//此Comment属于Article空间的元素
class Comment { }
//创建一个名为&#39;MessageBoard&#39;的命名空间
namespace MessageBoard;
//此Comment属于MessageBoard空间的元素
class Comment { }
?>
Copy after login

You cannot directly call other elements between different spaces, you need to use the namespace syntax.

The sample code is as follows:

<?php
namespace Article;
class Comment { }
namespace MessageBoard;
class Comment { }
//调用当前空间(MessageBoard)的Comment类
$comment = new Comment();
//调用Article空间的Comment类
$article_comment = new \Article\Comment();
?>
Copy after login

You can see that when calling the Comment class in the article space in the MessageBoard space, a syntax like a file path is used: \Space name\Element name

Except for classes, the usage of functions and constants is the same. Below I created new elements for the two spaces and output them in the MessageBoard space. value.

The code is as follows:

<?php
namespace Article;
const PATH = &#39;/article&#39;;
function getCommentTotal() {
 return 100;
}
class Comment { }
namespace MessageBoard;
const PATH = &#39;/message_board&#39;;
function getCommentTotal() {
 return 300;
}
class Comment { }
//调用当前空间的常量、函数和类
echo PATH; //message_board
echo getCommentTotal(); //300
$comment = new Comment();
//调用Article空间的常量、函数和类
echo \Article\PATH; //article
echo \Article\getCommentTotal(); //100
$article_comment = new \Article\Comment();
?>
Copy after login

The following results will be obtained:/message_board300/article100


[Related tutorial recommendations]

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

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

3. A complete set of tutorials on PHP programming from entry to master

The above is the detailed content of Detailed explanation of the role of php namespace. 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