This article will introduce to you how PHP declares a namespace, three ways to access space elements, and how to introduce a 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 '我是中国人'; } getInfo(); namespace USA; // 定义命名空间 function getInfo () { echo '我是美国人'; } getInfo(); ?>
Effect:
Use
\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;"><?php
namespace China; // 定义命名空间
function getInfo () {
echo &#39;我是中国人&#39;;
}
getInfo();
namespace USA; // 定义命名空间
function getInfo () {
echo &#39;我是美国人&#39;;
}
getInfo();
\China\ getInfo();
?></pre><div class="contentsignin">Copy after login</div></div>
Effect:
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 'China\Beijing\Shunyi'; } getInfo(); // 非限定名称访问 namespace USA\Washington; // 定义命名空间 function getInfo () { echo 'USA\Washington'; } \USA\Washington\ getInfo(); // 完全限定名称访问 ?>
Effect:
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 'China\Beijing\Shunyi'; } namespace China\Beijing; // 定义命名空间 function getInfo () { echo 'China\Beijing'; } getInfo(); // 非限定名称访问 \China\Beijing\getInfo(); // 完全限定名称访问 Shunyi\getInfo(); // 限定名称访问 ?>
Effect:
2. Introduce the namespaceIntroduce namespace
Fully qualified name access element
Splicing rules for introducing namespaces
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:
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 '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:
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 '<br>'; $stu1=new ChinaStudent; var_dump($stu1); ?>
Effect:
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: 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: " 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!<?php
function getInfo() {
echo '李白<br>';
}
\getInfo();
?>