1.命名空间的意义
假设有下面两个文件:
include.php
1 <?php ?2 ????function test(){3 4 ????};5 ?>
test.php
1 <?php2 ????include "include.php";3 4 ????function test(){5 6 ????};7 ?>
当脚本编译时会提示这样一个错误:
Fatal error: Cannot redeclare test() (previously declared in E:\xampp\htdocs\test.php:6) in E:\xampp\htdocs\include.php on line 4
这是因为两个文件中的test()函数重名了。这就是重名问题。解决这个问题的一个方法就是更改名字。
在一个大的项目中,代码通常都是由不同的人编写最后组合在一起,重名问题常常发生。让他们更改函数名是非常繁琐的。
命名空间就是为了解决这个问题而出现的。
2.定义一个命名空间
/*以下属于testspace1命名空间*/ ???/*注:第一个命名空间之前不能有任何PHP代码不然会出错*/ ???namespace testspace1; ???echo __NAMESPACE__;echo "<br>"; ???echo "**************<br>"; ???/*以下属于testspace1命名空间*/ ???namespace testspace2; ???echo __NAMESPACE__;echo "<br>"; ???echo "**************<br>"; ???/*以下属于testspace1命名空间*/ ???namespace testspace3; ???echo __NAMESPACE__;echo "<br>"; ???echo "**************<br>";
命名空间开始与namespace语句,结束于新的namesapce语句或文件的结尾。
现在用命名空间解决上一个重名问题只需要这样改写代码就可以
include.php
1 <?php2 ????namespace includespace;3 ????function test(){4 ????????echo "i am from includespace <br>";5 ????}6 ?>
test.php
1 <?php 2 ????namespace testspace; 3 ?4 ????include "include.php"; 5 ????function test(){ 6 ????????echo "i am from testspace <br>"; 7 ????} 8 ?9 ????test();//输出i am from testspace10 ????\includespace\test();//输出i am from includespace11 ?>
没有设置命名空间的都同属于公共空间‘\‘。例如上一个例子,如果不给include.php设命名空间的话可以这样写
include.php
<?phpfunction test(){echo "i am from ‘\‘ <br>";}?>
test.php
<?phpnamespace testspace;include "include.php";function test(){echo "i am from testspace <br>";}test();//输出i am from testspace\test();//输出i am from ‘\‘?>
3.命名空间的层次结构
namespace grandfa; ???function test(){ ???????echo "grandfa"; ???} ???test();//grandfa非限定,自动匹配当前命名空间 ???father\test();//father,限定名称 ???father\son\test();//son,限定名称 ???\grandfa\test();//grandfa,完全限定名称 ???\grandfa\father\test();//father ???\grandfa\father\son\test();//son/***************************************/ ???namespace grandfa\father; ???// function test(){ ???// ????echo "father"; ???// }/**************************************/ ???namespace grandfa\father\son; ???function test(){ ???????echo "son"; ???}
4.导入命名空间和类
1 <?php 2 ????namespace grandfa; 3 ????function test(){ 4 ????????echo "grandfa"; 5 ????} 6 ?7 ????$son1 = new father\son\son; 8 /***************************************/ 9 ????namespace grandfa\father;10 ????function test(){11 ????????echo "father";12 ????}13 14 15 /**************************************/16 ????namespace grandfa\father\son;17 ????function test(){18 ????????echo "son";19 ????}20 21 ????class son{};22 23 ?>
如果我们想在grandfa的命名空间中调用son类,那么我们不得不在前面加上长长的前缀,在实际情况中,前缀很有可能要比这个长的多。
我们可以使用导入别名的方式简化使用
use \grandfa\father\son as S; ???$son1=new S\son;
这样一来就不用每次使用长长的前缀,直接使用别名S就可以了。
如果你还嫌不够简单,甚至可以直接导入一个类
use \grandfa\father\son as S; ???$son1=new S\son;
5.命名空间与常量
1 <?php 2 ????namespace grandfa; 3 ????define(‘DEA‘,‘1111‘); 4 ????define(‘DEB‘,‘2222‘); 5 ????const A=‘1111‘; 6 ????const B=‘2222‘; 7 ?8 ????echo A;echo B; 9 ????echo DEA;echo DEB;10 /***************************************/11 ????namespace grandfa\father;12 ????function test(){13 ????????echo "father";14 ????}15 16 ????echo \grandfa\A;echo \grandfa\B;17 ????echo DEA;echo DEB;18 /**************************************/19 ????namespace grandfa\father\son;20 ????function test(){21 ????????echo "son";22 ????}23 24 ????class son{};25 26 ????echo \grandfa\A;echo \grandfa\B;27 ????echo DEA;echo DEB;28 ?>
define定义的常量是全局的任何命名空间都可以访问,而const定义的常量是从属于其命名空间的,在其他命名空间访问需要加命名空间的限定
PHP-命名空间
原文地址:https://www.cnblogs.com/patermenkey/p/8311597.html