Use of php namespace

1. Namespace

1.1 Introduction

In a large project, you may encounter classes, functions, and constants with the same name. In order to distinguish these elements, we can store these elements in different namespaces.
1. The namespace is a package, used to store the classes, functions, and constants in the project.
2. The namespace is declared by the namespace keyword

1.2 Declaring namespace

Example:

<?php
  namespace China; // 定义命名空间
  function getInfo () {
    echo '我是中国人';
  }
  getInfo();

  namespace USA; // 定义命名空间
  function getInfo () {
    echo '我是美国人';
  }
  getInfo();
?>

Effect:
Insert picture description here
Use \命名空间\can enter a certain namespace to call methods.
example:

<?php
  namespace China; // 定义命名空间
  function getInfo () {
    
    
    echo '我是中国人';
  }
  getInfo();

  namespace USA; // 定义命名空间
  function getInfo () {
    
    
    echo '我是美国人';
  }
  getInfo();
  \China\ getInfo();
?>

effect:
Insert picture description here

1.3 Multi-level namespace

The name of the namespace can be multi-level (sub-namespace)
for example:

<?php
  namespace China\Beijing\Shunyi; // 定义命名空间
  function getInfo () {
    
    
    echo 'China\Beijing\Shunyi';
  }
  getInfo(); // 非限定名称访问

  namespace USA\Washington; // 定义命名空间
  function getInfo () {
    
    
    echo 'USA\Washington';
  }
  \USA\Washington\ getInfo(); // 完全限定名称访问
?>

effect:
Insert picture description here

1.4 Three ways to access spatial elements

1. Unqualified name access
2. Fully qualified name access
3. Qualified name access
Example:

<?php
  namespace China\Beijing\Shunyi; // 定义命名空间
  function getInfo () {
    
    
    echo 'China\Beijing\Shunyi';
  }

  namespace China\Beijing; // 定义命名空间
  function getInfo () {
    
    
    echo 'China\Beijing';
  }

  getInfo();  // 非限定名称访问 
  \China\Beijing\getInfo();  // 完全限定名称访问
  Shunyi\getInfo();  // 限定名称访问

?>

effect:
Insert picture description here

Two, the introduction of namespace

Access elements by useintroducing namespaces,
fully qualified names, and
introducing splicing rules for namespaces

Public space + introduced space + (remove the public part, the public part can only be left one level) space element

Example:

<?php
  namespace China\Beijing\Shunyi; // 定义命名空间
  function getInfo () {
    
    
    echo 'China\Beijing\Shunyi<br>';
  }

  namespace China\Beijing; // 定义命名空间
  function getInfo () {
    
    
    echo 'China\Beijing<br>';
  }

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

?>

effect:
Insert picture description here

2.1 Introducing spatial elements

Introduce class: use
import function: use function
import constant: use const
example:

<?php
  namespace China\Beijing\Shunyi; // 定义命名空间
  class Student {
    
    
  }
  function getInfo() {
    
    
    echo 'jdk';
  }
  const TYPE = 'CONST';
  namespace USA; // 定义命名空间
  // 引入类
  use China\Beijing\Shunyi\Student;
  $stu = new Student();
  var_dump($stu);
  echo '<br>';
  // 引入函数
  use function China\Beijing\Shunyi\getInfo;
  getInfo();
  echo '<br>';

  // 引入常量
  use const China\Beijing\Shunyi\TYPE;
  echo TYPE;
?>

effect:
Insert picture description here

2.2 Give aliases to classes and functions

If the imported class and function have the same name as the class and function in the current space, you need to alias the imported class and function.
Alias ​​by as
Example:

<?php
  namespace China\Beijing\Shunyi; // 定义命名空间
  class Student {
    
    
  }
  

  namespace USA; // 定义命名空间
  class Student {
    
    

  }
  use China\Beijing\Shunyi\Student as ChinaStudent;
  $stu=new Student;
  var_dump($stu);
  echo '<br>';
  $stu1=new ChinaStudent;
  var_dump($stu1);
?>

effect:
Insert picture description here

2.3 Public space

If a page is not element namespace declaration space this page in public space
public space with \representation
cases:

<?php
  function getInfo() {
    
    
    echo '李白<br>';
  }
  \getInfo();

?>

effect:
Insert picture description here

2.4 Namespace considerations

1. The namespace can only store classes, functions, and const constants.
2. There cannot be any code before the first namespace, blank characters, and header() also do not work.
3. The included file does not affect the current namespace.

On the way of learning php, if you think this article is helpful to you, please pay attention to like and comment Sanlian, thank you, you must be another support for my blog.

Guess you like

Origin blog.csdn.net/weixin_44103733/article/details/108773347