About the namespace of PHP

Source: Internet
Author: User
This article introduces the content is about the PHP namespace, has a certain reference value, now share to everyone, the need for friends can refer to

What is a PHP namespace

PHP Manual: In a broad sense, namespaces are a way of encapsulating things. This abstract concept can be seen in many places. For example, in an operating system, a directory is used to group related files, and for files in a directory, it plays the role of a namespace.

The role of namespaces

1. User-written code conflicts with the name of a class/function/constant or third-party class/function/constant within PHP.
2. Create an alias (or short) name for a very long identifier name (usually defined to alleviate the first type of problem) and improve the readability of the source code.

Example

Writing this namespace article is because a friend just learned this to ask me what a namespace is. I'm thinking about how to describe it briefly and it's easy to understand. Let me illustrate my own understanding of namespaces with a few simple examples.

1. Example One
First we set up two classes of 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 a index.php file to introduce the above two classes and invoke the methods in them.
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, you cannot re-declare the test class because you introduced two times, and the class names in the two files are the same, conflicting. At this point the namespace is needed to solve the problem, and it's easy.
2. Example II
We will now modify the two class files slightly.
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 namespaces. Now run index.php find no error, modify index.php to make method call test
index.php

Require_once ("a.php"); Require_once ("b.php"); $a = new a\test\test (); $a->test ();//Page output: This is a class.

3. example Three
Now there is another situation, such as I need to instantiate a.php in the test class multiple times, so every time we need to complete the namespace information is more difficult to do? Like what:
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 ();//Page output: This is a class.this is a class.

Although there is no error, but you will find it troublesome, every time you need to fully write the name of the namespace, although not error and can be ctrl+c,ctrl+v, but 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 ();//Page output: This is a class.this is a class.

The use keyword is used to introduce a class, using a namespace to represent a class. You can instantiate the operation directly later
4. example Five
Then another question came up, 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 was already in use ... Because the namespace is used, but the two classes have the same name, it is test, the program does not know that the second Test class is the test class in b.php, and 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 ();//Page output: This is A Class.this is B class. Perfect solution

The As keyword defines an alias for the class name, which effectively prevents the same class name from conflicting
5. Example VI
Here's another scenario, I'll give you a few code snippets, which are in the YII2 framework, which are not related to the framework, but are seen in many places for demonstration purposes.

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

It is clear that there is a yii class here, but why the front of a backslash "\", we first trace the Yii class, some students will ask how to trace it, if you are using the phpstorm editor, press CTRL directly, Mouse click on the class name will jump to such a class file, about how to use the Phpstorm editor, see: Phpstorm hack version and use tutorial
The following is the Yii class file code snippet:

/** * Yii bootstrap file. * * @link http://www.yiiframework.com/* @copyright Copyright (c) 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 a backslash "\" before the class is required
For example, we build a global class file at the a.php level: c.php:

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

You can 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 ();//Page output: This is C class.this is a class. This is a B class.this is C class.

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

What is a PHP namespace

PHP Manual: In a broad sense, namespaces are a way of encapsulating things. This abstract concept can be seen in many places. For example, in an operating system, a directory is used to group related files, and for files in a directory, it plays the role of a namespace.

The role of namespaces

1. User-written code conflicts with the name of a class/function/constant or third-party class/function/constant within PHP.
2. Create an alias (or short) name for a very long identifier name (usually defined to alleviate the first type of problem) and improve the readability of the source code.

Example

Writing this namespace article is because a friend just learned this to ask me what a namespace is. I'm thinking about how to describe it briefly and it's easy to understand. Let me illustrate my own understanding of namespaces with a few simple examples.

1. Example One
First we set up two classes of 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 a index.php file to introduce the above two classes and invoke the methods in them.
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, you cannot re-declare the test class because you introduced two times, and the class names in the two files are the same, conflicting. At this point the namespace is needed to solve the problem, and it's easy.
2. Example II
We will now modify the two class files slightly.
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 namespaces. Now run index.php find no error, modify index.php to make method call test
index.php

Require_once ("a.php"); Require_once ("b.php"); $a = new a\test\test (); $a->test ();//Page output: This is a class.

3. example Three
Now there is another situation, such as I need to instantiate a.php in the test class multiple times, so every time we need to complete the namespace information is more difficult to do? Like what:
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 ();//Page output: This is a class.this is a class.

Although there is no error, but you will find it troublesome, every time you need to fully write the name of the namespace, although not error and can be ctrl+c,ctrl+v, but 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 ();//Page output: This is a class.this is a class.

The use keyword is used to introduce a class, using a namespace to represent a class. You can instantiate the operation directly later
4. example Five
Then another question came up, 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 was already in use ... Because the namespace is used, but the two classes have the same name, it is test, the program does not know that the second Test class is the test class in b.php, and 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 ();//Page output: This is A Class.this is B class. Perfect solution

The As keyword defines an alias for the class name, which effectively prevents the same class name from conflicting
5. Example VI
Here's another scenario, I'll give you a few code snippets, which are in the YII2 framework, which are not related to the framework, but are seen in many places for demonstration purposes.

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

It is clear that there is a yii class here, but why the front of a backslash "\", we first trace the Yii class, some students will ask how to trace it, if you are using the phpstorm editor, press CTRL directly, Mouse click on the class name will jump to such a class file, about how to use the Phpstorm editor, see: Phpstorm hack version and use tutorial
The following is the Yii class file code snippet:

/** * Yii bootstrap file. * * @link http://www.yiiframework.com/* @copyright Copyright (c) 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 a backslash "\" before the class is required
For example, we build a global class file at the a.php level: c.php:

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

You can 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 ();//Page output: This is C class.this is a class. This is a B class.this is C class.

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.