PHP namespace

What is PHP namespace

PHP Manual: In a broad sense, a namespace is a way to encapsulate things. This abstraction can be seen in many places. For example, in operating systems a directory is used to group related files, and it acts as a namespace for files in a directory.

The role of namespaces

1. A name conflict between user-written code and a class/function/constant within PHP or a third-party class/function/constant.
2. Create an alias (or short) name for very long identifier names (usually defined to alleviate the first type of problems) to improve the readability of the source code.

Example

I wrote this namespace article because a friend just learned this and asked me what a namespace is. I was wondering how to describe it simply and understand it easily. Below I will use a few simple examples to illustrate my own understanding of namespaces.

1. Example 1
First we create two class files
a.php

class Test
{
    public function test()
    {
        echo "this is A class.";
    }
}

b.php

class Test
{
    public function test()
    {
        echo "this is B class.";
    }
}

Create another index.php file to introduce the above two classes and call their methods.
index.php

require_once("a.php");
require_once("b.php");

Now run the index.php file and you will find a fatal error: Fatal error: Cannot redeclare class Test in. . . Obviously, the Test class cannot be re-declared, because you introduced it twice, and the class name in the two files is the same, which conflicts. At this time, namespaces are needed to solve this problem, and it is easy.
2. Example 2
We now slightly modify the two class files.
a.php

namespace a\test;

class Test
{
    public function test()
    {
        echo "this is A class.";
    }
}

b.php

namespace b\test;

class Test
{
    public function test()
    {
        echo "this is B class.";
    }
}

The namespace keyword is used to declare a namespace. Now run index.php and find that there is no error, modify index.php to test the method call
index.php

require_once("a.php");
require_once("b.php");

$a = new a\test\Test();
$a->test();

//页面输出:this is A class.

3. Example 3
Now there is another situation. For example, I need to instantiate the Test class in a.php for many times. What if we need to write the complete namespace information every time? For example:
index.php

require_once("a.php");
require_once("b.php");

$a = new a\test\Test();
$a_a = new a\test\Test();
$a_b = new a\test\Test();
$a->test();
$a_a->test();

//页面输出:this is A class.this is A class.

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

require_once("a.php");
require_once("b.php");

use a\test\Test;

$a = new Test();
$a_a = new Test();
$a_b = new Test();
$a->test();
$a_a->test();

//页面输出:this is A class.this is A class.

The use keyword is used to introduce a class and use a namespace to indicate that a class is used. After that, you can directly instantiate the operation
4. Example 5
Next , another problem comes again, as follows:
index.php

require_once("a.php");
require_once("b.php");

use a\test\Test;
use b\test\Test;

$a = new Test();
$b = new Test();
$a->test();
$b->test();

Obviously, another fatal error: Fatal error: Cannot use b\test\Test as Test because the name is already in use in . . . Because although the namespace is used, the two classes have the same name, both of which 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,
such as:
index.php

require_once("a.php");
require_once("b.php");

use a\test\Test;
use b\test\Test as BTest;

$a = new Test();
$b = new BTest();
$a->test();
$b->test();
//页面输出:this is A class.this is B class.完美解决


The
as keyword defines an alias for the class name, which can effectively prevent the conflict of the same class name . This example can be seen in many places.

if (\Yii::$app->request->isPost) {
            $post = \Yii::$app->request->post();
           ...
        }

Obviously, a Yii class is used here, but why is there a backslash "\" in front of it? Let's trace the Yii class first. Some students will ask how to trace it. If you are using the PHPstorm editor, press and hold Ctrl directly. , the mouse click on the class name will jump to such class file , for how to use the PHPstorm editor, please check: PhpStorm cracked version and usage tutorial
The following is the Yii class file code segment:

/**
 * Yii bootstrap file.
 *
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */
require(__DIR__ . '/BaseYii.php');

class Yii extends \yii\BaseYii
{
}

You will find that the Yii class does not have a namespace. We call this class a global class. If you want to use it, you need to add a backslash "\" in front of the class. For
example, we create a global class file at the same level as a.php: c.php :

class Test
{
    public function test()
    {
        echo "this is C class.";
    }
}

Do this in the index.php file to call the test method in c.php

require_once("a.php");
require_once("b.php");
require_once("c.php");

use a\test\Test;
use b\test\Test as BTest;

$a = new Test();
$b = new BTest();
$c = new \Test();
$a->test();
$b->test();
$c->test();
//页面输出:this is C class.this is A class.this is B class.this is C class.

Note: keyword usage such as namespace, use, as and usage of global classes.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324474206&siteId=291194637