Home > Backend Development > PHP Tutorial > An in-depth analysis of how to declare and introduce namespaces in PHP

An in-depth analysis of how to declare and introduce namespaces in PHP

青灯夜游
Release: 2023-04-10 12:02:02
forward
3479 people have browsed it

This article will introduce to you how PHP declares a namespace, three ways to access space elements, and how to introduce a namespace.

An in-depth analysis of how to declare and introduce namespaces in PHP

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. Namespace is a package, used to store classes, functions, and constants in the project 2. Declare the namespace through the namespace keyword

##1.2 Declare the namespaceExample:

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

  namespace USA; // 定义命名空间
  function getInfo () {
    echo &#39;我是美国人&#39;;
  }
  getInfo();
?>
Copy after login

Effect:

An in-depth analysis of how to declare and introduce namespaces in PHPUse

\namespace\

to enter a namespace and call methods. Example: <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">&lt;?php namespace China; // 定义命名空间 function getInfo () { echo &amp;#39;我是中国人&amp;#39;; } getInfo(); namespace USA; // 定义命名空间 function getInfo () { echo &amp;#39;我是美国人&amp;#39;; } getInfo(); \China\ getInfo(); ?&gt;</pre><div class="contentsignin">Copy after login</div></div> Effect:

An in-depth analysis of how to declare and introduce namespaces in PHP

1.3 Multi-level namespace Naming The name of the space can be multi-level (sub-level namespace) For example:

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

  namespace USA\Washington; // 定义命名空间
  function getInfo () {
    echo &#39;USA\Washington&#39;;
  }
  \USA\Washington\ getInfo(); // 完全限定名称访问
?>
Copy after login

Effect:

An in-depth analysis of how to declare and introduce namespaces in PHP

1.4 Three ways to access space elements1. Unqualified name access 2. Fully qualified name access 3. Restricted name access Example:

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

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

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

?>
Copy after login

Effect:

An in-depth analysis of how to declare and introduce namespaces in PHP

2. Introduce the namespace

through

use

Introduce namespace Fully qualified name access element Splicing rules for introducing namespaces

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

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

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

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

?>
Copy after login

Effect:

An in-depth analysis of how to declare and introduce namespaces in PHP

2.1 Introducing space elementsIntroduction class: use Introduce functions: use function Introduce constants: use const Example:

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

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

Effect:

An in-depth analysis of how to declare and introduce namespaces in PHP

2.2 Give aliases to classes and functionsIf the introduced classes and functions have the same names as those in the current space, you need to give the introduced classes and functions aliases. Use alias 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 &#39;<br>&#39;;
  $stu1=new ChinaStudent;
  var_dump($stu1);
?>
Copy after login

Effect:

An in-depth analysis of how to declare and introduce namespaces in PHP

2.3 Public spaceIf a page There is no namespace declaration space. The elements of this page are in the public space. Public spaces are represented by

\

Example:

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

?>
Copy after login
Effect:

An in-depth analysis of how to declare and introduce namespaces in PHP

2.4 Namespace considerations1 , The namespace can only store classes, functions, and const constants. 2. There cannot be any code in front of the first namespace, including blank characters and header(). 3. Included files do not affect the current namespace.

Recommended learning: "

PHP Video Tutorial

"

The above is the detailed content of An in-depth analysis of how to declare and introduce namespaces in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:juejin.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template