Introduction to PHP namespaces

不言
Release: 2023-04-02 13:32:02
Original
1180 people have browsed it

This article mainly introduces the namespace of PHP, which has certain reference value. Now I share it with everyone. Friends in need can refer to it

What is a PHP namespace?

(PHP 5 >= 5.3.0, PHP 7)

What is a namespace? Broadly speaking, a namespace is a way 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 not in the same directory. There are two foo.txt files. In addition, 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.

In PHP, namespaces are used to solve two types of problems encountered when creating reusable code such as classes or functions when writing class libraries or applications:

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

  2. Create an alias (or short) name for a very long identifier name (usually defined to alleviate the first type of problem), improve the source Code readability.

Although any legal PHP code can be included in a namespace, only the following types of code are affected by the namespace, which are: classes (including abstract classes and traits), interfaces, functions and constants.

The namespace is declared by the keyword namespace. If a file contains a namespace, it must declare the namespace before all other code except one: the declare keyword.

The only legal code before declaring a namespace is the declare statement used to define the source file encoding. In addition, all non-PHP code, including whitespace, cannot appear before the namespace declaration:

<html>
<?php
namespace MyProject; // 致命错误 - 命名空间必须是程序脚本的第一条语句?>
Copy after login

In addition, unlike other language features of PHP, the same namespace can be defined in multiple files, which allows Split the contents of the same namespace into different files.

1. Example 1
First we create two class files

a.php

<?phpclass Test
{    public function ceshi(){        echo __FILE__;
    }
}
Copy after login
Copy after login

b.php

<?phpclass Test
{    public function ceshi(){        echo __FILE__;
    }
}
Copy after login
Copy after login

index.php

<?php
require_once("a.php");
require_once("b.php");
Copy after login

Now run the index.php file

You will find a fatal error: Fatal error: Cannot redeclare class Test in. . . Obviously, the Test class cannot be redeclared because you introduced it twice, and the class names in the two files are the same, which conflicts. At this time, namespace is needed to solve this problem, and it is easy.

2. Example 2
We now slightly modify the two class files.

a.php

<?php
namespace demo1\a\Test;class Test
{    public function ceshi(){        echo __FILE__;
    }
}
Copy after login

b.php

<?php
namespace demo1\b\Test;class Test
{    public function ceshi(){        echo __FILE__;
    }
}
Copy after login

The namespace keyword is used to declare the namespace of. Now run index.php and find no errors. Modify index.php to perform method call test

index.php

<?php
require_once("a.php");
require_once("b.php");
$a1 = new demo1\a\Test\Test();
$a1->ceshi();
Copy after login

Now run index.php File

D:\phpStudy\WWW\demo\demo1\a.php

3. Example 3

Now there is another For example, if I need to instantiate the Test class in a.php multiple times, what should I do if it is troublesome to write the complete namespace information every time? For example:

<?php
require_once("a.php");
require_once("b.php");
$a1 = new demo1\a\Test\Test();
$a2 = new demo1\a\Test\Test();
$a1->ceshi();echo '
'; $a2->ceshi();
Copy after login

Although there is no error, you will find it more troublesome. You need to write the full namespace name every time. Although no error is reported and ctrl c, ctrl v can be used, it is not very beautiful (^_^ ).

You can do this

index.php

<?php
require_once("a.php");
require_once("b.php");
use demo1\a\Test\Test;
$a1 = new Test();
$a2 = new Test();
$a1->ceshi();
echo '
'; $a2->ceshi();
Copy after login

The use keyword is used to introduce classes and is represented by namespaces A certain class is used. You can directly instantiate the operation later

4. Example 5

Then another question comes again, as follows:

index.php

<?php
require_once("a.php");
require_once("b.php");
use demo1\a\Test\Test;
use demo1\b\Test\Test;
$a = new Test();
$b = new Test();
$a->ceshi();
echo '
'; $b->ceshi();
Copy after login

Now run the index.php file

## Fatal error: Cannot use demo1\b\Test\Test as Test because the name is already in use in D:\phpStudy\WWW\demo\demo1\index.php on line 5

Because although the namespace is used, However, the two classes have the same name, both are Test. The program does not know that the second Test class is the Test class in b.php. At this time, you use the as keyword

index .php

<?php
require_once("a.php");
require_once("b.php");
use demo1\a\Test\Test;
use demo1\b\Test\Test as bTest;
$a = new Test();$b = new bTest();
$a->ceshi();echo '
'; $b->ceshi();
Copy after login

The as keyword defines an alias for the class name, which can effectively prevent conflicts with the same class name

5. Example 6

For example, we create a global class file at the same level as a.php: c.php:

c.php

<?php
class Test{    
public function ceshi(){        
echo __FILE__;
    }
}
Copy after login

在index.php文件中这样做即可调用c.php中的test方法

<?php
require_once("a.php");
require_once("b.php");
require_once("c.php");
use demo1\a\Test\Test;
use demo1\b\Test\Test as bTest;
$a = new Test();$a->ceshi();
echo '
'; $b = new bTest(); $b->ceshi(); echo '
'; $c = new \Test(); $c->ceshi(); echo '
';
Copy after login

我们将这种类叫做全局类,如果要使用需要类前面加入反斜杠”\” 

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

相关推荐:

关于PHP多态的理解

PHP文件编程的介绍

The above is the detailed content of Introduction to PHP namespaces. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!