Summary of the Usage of PHP Command Space namespace and use

  • 2021-08-21 20:01:25
  • OfStack

The most explicit purpose of Namespace 1 is to solve the problem of duplicate names. PHP does not allow two functions or classes to have the same name, otherwise a fatal error will be generated. In this case, it can be solved by avoiding naming duplication. The most common way is to agree on a prefix.

Purpose of using namespace:

When working on a team project, avoid conflicts with classes created by other team members; When an individual is in charge of a project, avoid conflicts between new classes before and after;

According to my understanding, when using the required classes, require or include needs to be introduced first, so the premise of class redefinition error is that two classes with the same name have been introduced. At present, some php frameworks will automatically load (that is, include) all new model classes, so in order to avoid duplicate name conflicts between your new model class and the native core classes of the project framework, namespace is adopted. (After thinking about it, conflicts with new classes created by team members should be avoided through communication. Even after the incident, the immediate maintenance of class names should be readjusted to avoid the complexity of maintenance caused by confusion of class understanding in the later period)

Let's take a step forward to understand its purpose in combination with the usage method.

How to use namespace:

For testing, I will create three files: 1. php and name. php (this file is used to perform testing), which will not be explained later. Please pay attention to the code changes.

1. The definition of naming after 1. namespace is case insensitive

namespace one;
namespace One;
namespace ONE;

As above, you can choose one as your own specification. (I use the first one to test the following code)

2. If no namespace is defined, it is understood that the top-level namespace is used. new class, you can prefix the class with a backslash\ or not.


//1.php 
class Person{
 function __construct(){
  echo 'I am one!';
 }
}
//name.php
require_once './1.php';
new Person(); // Output  I am one!;
new \Person(); // Output  I am one!; 

3. In the new class, the backslash character is used instead of the forward slash between 1 when namespace is taken.

Memory method: According to the order of finding slashes in%, it is understood as forward slash. (Sometimes I say backslash, I don't know which direction it is. I used to remember it in the direction of rising from left to right, but now I feel that this is too unreliable)


//name.php
require_once './1.php';
new /Person(); //  Code error reporting: Parse error: syntax error, unexpected '/'

4. Class in the specified namespace, new class, 1 must take the specified namespace.

Without the specified namespace, according to point 2, php will look for this class from the top-level namespace. Remember: This cannot be understood according to the fact that the top-level namespace contains 1 and other namespaces. Instead, the top-level namespace should be completely distinguished from other namespaces.


//1.php 
namespace one;
class Person{
 function __construct(){
  echo 'I am one!';
 }
}
//name.php
require_once './1.php';
new \one\Person(); // Output  I am one!;
new \Person(); // Code error reporting: Fatal error: Class 'Person' not found

Take this popular example to understand: take the specified namespace to represent someone's apple (in his hand), and the top namespace represents the apple in the apple box (in the box). If you want to find someone's apple now, take someone's namespace with you, otherwise you will find someone's apple in the box, and of course you can't find it.

5. The code after namespace declaration belongs to this namespace, even if there is include or require, it will not affect (the emphasis is on the understanding of the second half of the sentence, see the code for details).


//1.php 
namespace one;
class Person{
 function __construct(){
  echo 'I am one!';
 }
}
//name.php
namespace test;
require './1.php'; 
new \one\Person(); // Output  I am one!;
new Person(); // What will the result be here? Guess 

The result of the last line is reported as an error:


Fatal error: Class 'test\Person' not found

First of all, compare it with point 2 here:

The second point, I said, when there is no namespace, new class, there is no backslash meaning 1 sample.

Here, with namespaces, the meaning of having and not having backslashes is different.

The last line is replaced by


new \Person(); 

Error report of result:


Fatal error: Class 'Person' not found

Then, let's talk about the current point.

We can see that the namespace corresponding to the last line of code is test, which is not affected by the namespace in the require file.

One step further to enhance validation, I modified the name. php file as follows:


//name.php
namespace test;
require './1.php'; 
class Person{
 function __construct(){
  echo 'I am test!';
 }
}
new \one\Person(); // Output  I am one!;
new Person(); // What will be the result here? Guess for yourself  

Finally, this example refreshes my understanding of require.

According to my previous understanding of require, before the PHP program is executed, the file specified by require will be read in, making it a part of the PHP program web page. So I often simply understand it as replacement, just putting the extracted code back in place. Then I tried to put the contents of the 1. php file into name. php:


//name.php
namespace test;
namespace one;
class Person{
 function __construct(){
  echo 'I am one!';
 }
}
class Person{
 function __construct(){
  echo 'I am test!';
 }
}

Without the new class, the file will report an error:

Fatal error: Cannot redeclare class one\Person

It seems that simply understanding require as a replacement will not work here.

6. namespace does not contain the class name, and even if there is a part with the same name as the class name, it does not represent a class. new class, you still have to take this part with you.


//name.php
namespace test\person;
class Person{
 function __construct(){
  echo 'I am test!';
 }
}new \test\person\Person();  // Namespace person Cannot represent class name 

However, this is purely gilding the lily, so simply point out that it is good not to bring class names in namespace.

7.1 Multiple namespaces can exist in an php file, and no code can precede the first namespace.

Just say that you can't have any code before the first namespace, and you can have code before the next namespace. This can be tested by itself.


//name.php
require_once './1.php';
new /Person(); //  Code error reporting: Parse error: syntax error, unexpected '/'
0

The php namespace namespace is in paragraph 1, so let's talk about the purpose of use.

Purpose of using use:

When the namespace string is too long, use can shorten the namespace accordingly.

How to use use:

1. For the 1. new class, no backslash is required at the front. In addition, when there is no as after use, the shortened namespace defaults to the content after the last 1 backslash.


//name.php
require_once './1.php';
new /Person(); //  Code error reporting: Parse error: syntax error, unexpected '/'
1

By comparing A and B lines of code, it is necessary to note:

After using use, there is no backslash at the front of new class.

When use is not used, the namespace has a backslash at the front

By comparing a and b lines of code, we can understand:

When there is no as after use, the shortened namespace defaults to the content after the last 1 backslash. As above:

use animal\dog;

Equivalent to

use animal\dog as dog;

2. It is not recommended to add class names after namespace, but it is OK after use.


//name.php
require_once './1.php';
new /Person(); //  Code error reporting: Parse error: syntax error, unexpected '/'
2

As shown above, adding the class name after use is equivalent to changing the name of the class from Life to dog.

If you don't use as dog above, you will report an error:

Fatal error: Cannot use animal\dog\Life as Life because the name is already in use
Because there is also an Life class with a sample name under cat.

It can be understood that after using use, the class corresponding to this nickname can only be occupied by the current namespace, and it is not allowed to exist in other namespaces.


//name.php
namespace animal\dog;
class Life{
 function __construct(){
  echo 'dog life!';
 }
}
class Dog{
 function __construct(){
  echo 'dog in dog!';
 }
}
namespace animal\cat;
// class Dog{
// function __construct(){
//  echo 'dog in cat!';
//  }
// }
class Life{
 function __construct(){
  echo 'cat life!';
 }
}
use animal\dog; 
new dog\Dog(); 

As above, the

use animal\dog;
cat

With the above code, the purpose of using use (shortening the namespace name) is obvious.

Brief summary 1:

namespace is the function of dividing domains, which means that these things belong to a namespace.

use is a nickname, which can save a lot of things both in writing and speaking.


Related articles: