Namespaces

1.1 introduction

In a large project, we may encounter classes, functions, and constants with the same name. To distinguish these elements, we can store them in different namespaces. 1. A namespace is a package that holds classes, functions, and constants in a project

1.2 Declaring a namespace

Ex. :

<? php namespace China; Function getInfo () {echo 'I'm Chinese '; } getInfo(); namespace USA; Function getInfo () {echo 'I'm American '; } getInfo(); ? >Copy the code

Effect:usingnamespaceMethods can be called from a namespace. Ex. :


      
  namespace China; // Define the namespace
  function getInfo () {
    echo 'I am Chinese';
  }
  getInfo();

  namespace USA; // Define the namespace
  function getInfo () {
    echo 'I'm American';
  }
  getInfo();
  \China\ getInfo();
? >
Copy the code

Effect:

1.3 Multi-level namespaces

Namespace names can be multilevel (child namespaces) for example:


      
  namespace China\Beijing\Shunyi; // Define the namespace
  function getInfo () {
    echo 'China\Beijing\Shunyi';
  }
  getInfo(); // Unqualified name access

  namespace USA\Washington; // Define the namespace
  function getInfo () {
    echo 'USA\Washington';
  }
  \USA\Washington\ getInfo(); // Fully qualified name access
? >
Copy the code

Effect:

1.4 Three ways to access spatial elements

1, unqualified name access 2, fully qualified name access 3, qualified name access example:


      
  namespace China\Beijing\Shunyi; // Define the namespace
  function getInfo () {
    echo 'China\Beijing\Shunyi';
  }

  namespace China\Beijing; // Define the namespace
  function getInfo () {
    echo 'China\Beijing';
  }

  getInfo();  // Unqualified name access
  \China\Beijing\getInfo();  // Fully qualified name access
  Shunyi\getInfo();  // Qualify name access

? >
Copy the code

Effect:

2. Introduce namespaces

The use introduction namespace fully qualifies name access elements to introduce concatenation rules for the namespace

Public space + Introduced space + (remove the public part, the public part can only stay one level) space element

Ex. :


      
  namespace China\Beijing\Shunyi; // Define the namespace
  function getInfo () {
    echo 'China\Beijing\Shunyi<br>';
  }

  namespace China\Beijing; // Define the namespace
  function getInfo () {
    echo 'China\Beijing<br>';
  }

  use China\Beijing\Shunyi;
  getInfo(); 
  Shunyi\getInfo();

? >
Copy the code

Effect:

2.1 Introduction of spatial elements

Use const; use const; use const;


      
  namespace China\Beijing\Shunyi; // Define the namespace
  class Student {}function getInfo() {
    echo 'jdk';
  }
  const TYPE = 'CONST';
  namespace USA; // Define the namespace
  / / into the class
  use China\Beijing\Shunyi\Student;
  $stu = new Student();
  var_dump($stu);
  echo '<br>';
  // Introduce the function
  use function China\Beijing\Shunyi\getInfo;
  getInfo();
  echo '<br>';

  // Introduce constants
  use const China\Beijing\Shunyi\TYPE;
  echo TYPE;
? >
Copy the code

Effect:

2.2 Alias classes and functions

If the introduced classes and functions have the same name as those in the current space, you need to alias the introduced classes and functions. For example, using the alias as:


      
  namespace China\Beijing\Shunyi; // Define the namespace
  class Student {}namespace USA; // Define the namespace
  class Student {}use China\Beijing\Shunyi\Student as ChinaStudent;
  $stu=new Student;
  var_dump($stu);
  echo '<br>';
  $stu1=new ChinaStudent;
  var_dump($stu1);
? >
Copy the code

Effect:

2.3 Public Space

If a page does not have a namespace declaration space, the elements of the page are in public space.


      
  function getInfo() {
    echo "Li bai < br > ';
  }
  \getInfo();

? >
Copy the code

Effect:

2.4 Namespace Precautions

A namespace can only hold classes, functions, and const constants. 2. The first namespace must not be preceded by any code, whitespace, or header(). 3. Including files does not affect the current namespace.

On the way to learning PHP, if you find this article helpful to you, then please pay attention to like comment 3 times, thank you, your must be another support of my blog.