PHP Namespace namespace Definition and Import use Usage Detailed Explanation

  • 2021-09-12 00:45:33
  • OfStack

This article illustrates the definition of PHP namespace namespace and the usage of importing use. Share it for your reference, as follows:

In PHP, functions or classes with the same name are not allowed. To prevent duplicate conflicts between class names or function names defined by programmers in projects, the 1 concept of namespace was introduced in PHP 5.3.

1. Namespace, that is, the code is divided into different spaces, and the class names in different spaces are independent of each other and do not conflict with each other. Multiple namespaces can exist in an php file, and no code can precede the first namespace. The code declared by the content space belongs to this namespace, for example:


<?php
echo 111;  // Due to namespace There is a code before and an error is reported 
namespace Teacher;
class Person{
  function __construct(){
   echo 'Please study!';
  }
}

2. Namespace should be specified when calling classes or methods in different spaces. For example:


<?php
namespace Teacher;
class Person{
  function __construct(){
   echo 'Please study!<br/>';
  }
}
function Person(){
  return 'You must stay here!';
};
namespace Student;
class Person{
  function __construct(){
   echo 'I want to play!<br/>';
  }
}
new Person();     // This space ( Student Space) 
new \Teacher\Person();   //Teacher Space 
new \Student\Person();   //Student Space 
echo \Teacher\Person();   //Teacher Under space Person Function 

Output:


I want to play!
Please study!
I want to play!
You must stay here!

3. Introducing other files into a namespace does not belong to this namespace, but to the public space or the namespace defined in the file itself. Example:

First define 1. php and 2. php files:


<?php  //1.php
class Person{
 function __construct(){
   echo 'I am one!<br/>';
  }
}


<?php
namespace Newer;
require_once './1.php';
new Person();  // Error reported, can not be found Person;
new \Person();  // Output  I am tow!;


<?php  //2.php
namespace Two
class Person{
 function __construct(){
   echo 'I am tow!<br/>';
  }
}


<?php
namespace New;
require_once './2.php';
new Person();  // Report an error, ( Current space ) Can't find Person;
new \Person();  // Report an error, ( Public space ) Can't find Person;
new \Two\Person(); // Output  I am tow!;

4. Let's look at how to use use: (use can be referred to in abbreviation later)


namespace School\Parents;
class Man{
  function __construct(){
   echo 'Listen to teachers!<br/>';
  }
}
namespace School\Teacher;
class Person{
  function __construct(){
   echo 'Please study!<br/>';
  }
}
namespace School\Student;
class Person{
  function __construct(){
   echo 'I want to play!<br/>';
  }
}
new Person();     // Output I want to play!
new \School\Teacher\Person(); // Output Please study!
new Teacher\Person();   // Report an error 
----------
use School\Teacher;
new Teacher\Person();   // Output Please study!
----------
use School\Teacher as Tc;
new Tc\Person();   // Output Please study!
----------
use \School\Teacher\Person;
new Person();   // Report an error 
----------
use \School\Parent\Man;
new Man();   // Report an error 

For more readers interested in PHP related content, please check the topics on this site: "Introduction to php Object-Oriented Programming", "Introduction to PHP Basic Syntax", "Summary of PHP Operation and Operator Usage", "Summary of PHP Network Programming Skills", "Encyclopedia of PHP Array (Array) Operation Skills", "Summary of php String (string) Usage", "Introduction to php+mysql Database Operation Skills" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: