How to use namespaces in PHP

清浅
Release: 2023-04-05 06:00:01
Original
5416 people have browsed it

Namespace in PHP is a way to solve code conflicts caused by having the same class or method. Put PHP code into a namespace by using the namespace command. When you want to call a class, you must first call the namespace

When we write a PHP program, we will find that the program will fail because there is a class or method with the same name. However, the namespace in PHP can help us solve the code conflict problem. Next, in the article, we will introduce in detail how to use the namespace code

How to use namespaces in PHP

[Recommended Course: PHP Tutorial]

There are two files in the following example, one Demo.php and one index.php. The two files are in the same directory; write the namespace and Demo class in the Demo.php file, and index.php calls the Demo class in Demo.php; the "output result" in the following example means that the browser accesses index.php.

Demo.php file code

<?php
namespace DemoNameSpace;
 
class Demo {
    private $mysqlHandle;
 
    public function __construct() {
        echo &#39;This is namespace of PHP demo ,The Demo magic constant "__NAMESPACE__" is &#39;.__NAMESPACE__;
    }
}
?>
Copy after login

index.php file code

<?php
    include &#39;Demo.php&#39;;
    use DemoNameSpace\Demo;
    $DemoObj = new Demo();
?>
Copy after login

Output result:

“This is namespace of PHP demo ,The Demo magic constant "__NAMESPACE__" is DemoNameSpace”
Copy after login

Explanation of the above example: There is a _NAMESPACE__ magic constant in Demo.php; "It contains the string of the current namespace name. In global, code not included in any namespace, It contains an empty string."

Do not change Demo.php, change the index.php file

<?php
    include &#39;Demo.php&#39;;
    $Demo = new Demo();
?>
Copy after login

Output result:

“Fatal error: Class &#39;Demo&#39; not found in F:\JJserver\demo\index.php on line 4”
Copy after login

This is a common "fatal error" message. According to conventional PHP programming ideas, the output here should be consistent with the output above, but here it has a fatal error. But when we remove (or comment out) the statement "namespace DemoNameSpace;" in the Demo.php file, it becomes normal. This is the most common way we usually write classes and call classes.

Summary:

Comparing the two output situations of using namespace and not using namespace, and adding the definition of namespace to understand, the above fatal error situation is good. Got it. A namespace is defined in Demo.php, that is, after the namespace, the Demo class is defined, and then the Demo class is merged into the DemoNameSpace namespace. So when you want to call this Demo class, you must first call this DemoNameSpace namespace, that is, use the "useDemoNameSpace\Demo" statement in the index.php file

[Related recommendations: Naming in PHP What is space


The above is the detailed content of How to use namespaces 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
Latest Articles by Author
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!