Introduction of namespace

1. Use alias for namespace name

That is to introduce other namespaces into the namespace and use the use operator to implement them.

After the introduction of other namespaces, you can simply prefix the element with a subnamespace, instead of writing all the introduced space names

  1. <?php  
  2. namespace app1\pro1\item1;   
  3. header("content-type:text/html;charset=utf-8");  
  4. function func(){  
  5.     echo "func1";  
  6. }  
  7. const x = "name1";  
  8. class Fruit{  
  9.     public $a = 'Apple 1';  
  10.     static $b = 'Pineapple 1';  
  11. }  
  12.   
  13. namespace app2\pro2\item2\topic2;  
  14. function func(){  
  15.     echo "func2";  
  16. }  
  17. const x = "name2";  
  18. class Fruit{  
  19.     public $a = 'Apple 2';  
  20.     static $b = 'Pineapple 2';  
  21. }  
  22.   
  23. use app1\pro1\item1; //Bring in namespace  
  24. item1\func(); //func1  
  25. echo item1\x; //name1  
  26. echo item1\Fruit::$b//Pineapple 1  
  27. $fruit = new item1\Fruit;  
  28. echo $fruit->a; //Apple 1  

Namespace app1\pro1\item1 is introduced in namespace app2\pro2\item2\topic2, and the subspace name item1 is added before the element when it is used

2. Use alias for class name

Classes are widely used in php, which provides a way to introduce classes in a namespace.

  1. <?php  
  2. namespace app1\pro1\item1;   
  3. header("content-type:text/html;charset=utf-8");  
  4. function func(){  
  5.     echo "func1";  
  6. }  
  7. const x = "name1";  
  8. class Fruit{  
  9.     public $a = 'Apple 1';  
  10.     static $b = 'Pineapple 1';  
  11. }  
  12.   
  13. namespace app2\pro2\item2\topic2;  
  14. function func(){  
  15.     echo "func2";  
  16. }  
  17. const x = "name2";  
  18.   
  19. use app1\pro1\item1\Fruit; //Introducing classes in a namespace  
  20. func(); //func2  
  21. echo x; //name2  
  22. //Class elements using the introduced namespace  
  23. echo Fruit::$b//Pineapple 1  
  24. $fruit = new Fruit;  
  25. echo $fruit->a; //Apple 1  
The class Fruit is defined in the namespace app1\pro1\item1. After the class of the previous namespace is introduced in the namespace app2\pro2\item2\topic2, the class element can be called directly

3. Introduce other namespaces into the public namespace

In php, the space where the elements without a namespace are located is the public namespace, and the way that the public namespace is introduced into other namespaces is include(), and the path is filled in brackets.

Calls without a clear prefix will look for the element in the current space. If they cannot find it in the namespace, an error will be reported;

A single \ prefix accesses the func() function in the public namespace;

Use elements of the introduced namespace, use fully qualified names

For example:

Define a test1.php file as follows:

  1. <?php  
  2. namespace app1\pro1\item1;  
  3. header("content-type:text/html;charset=utf-8");  
  4. function func(){  
  5.     echo "func1";  
  6. }  
  7. const x = "name1";  
  8. class Fruit {  
  9.     public $a = "Apple 1";  
  10.     static $b = "Pineapple 1";  
  11. }  

Create a new test.php file in the same level directory, and introduce test1.php:

  1. <?php  
  2. header("content-type:text/html;charset=utf-8");  
  3. function func(){  
  4.     echo "func";  
  5. }  
  6. const x = "name";  
  7. class Fruit{  
  8.     public $a = "Apple";  
  9.     static $b = "pineapple";  
  10. }  
  11. include("./test1.php");  
  12. func(); //func   
  13. \func(); //func   
  14. \app1\pro1\item1\func(); //func1  
  15.   
  16.   
  17. echo x; //name  
  18. echo \x; //name  
  19. echo \app1\pro1\item1\x; //name1  
  20.   
  21.   
  22. echo Fruit::$b//pineapple  
  23. $fruit = new Fruit;  
  24. echo $fruit->a; //Apple  
  25.   
  26.   
  27. echo \app1\pro1\item1\Fruit::$b//Pineapple 1  
  28. $fruit1 = new \app1\pro1\item1\Fruit;  
  29. echo $fruit1->a; //Apple 1  

Tags: PHP

Posted on Sat, 14 Mar 2020 08:16:19 -0700 by haolan