假设有下面两个文件:php
include.php 函数
1 <?php 2 function test(){ 3 4 }; 5 ?>
test.phpspa
1 <?php 2 include "include.php"; 3 4 function test(){ 5 6 }; 7 ?>
当脚本编译时会提示这样一个错误:code
Fatal error: Cannot redeclare test() (previously declared in E:\xampp\htdocs\test.php:6) in E:\xampp\htdocs\include.php on line 4blog
这是由于两个文件中的test()函数重名了。这就是重名问题。解决这个问题的一个方法就是更更名字。io
在一个大的项目中,代码一般都是由不一样的人编写最后组合在一块儿,重名问题经常发生。让他们更改函数名是很是繁琐的。编译
命名空间就是为了解决这个问题而出现的。function
/*如下属于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语句或文件的结尾。class
如今用命名空间解决上一个重名问题只须要这样改写代码就能够test
include.php
1 <?php 2 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 testspace 10 \includespace\test();//输出i am from includespace 11 ?>
没有设置命名空间的都同属于公共空间'\'。例如上一个例子,若是不给include.php设命名空间的话能够这样写
include.php
<?php function test(){ echo "i am from '\' <br>"; } ?>
test.php
<?php namespace testspace; include "include.php"; function test(){ echo "i am from testspace <br>"; } test();//输出i am from testspace \test();//输出i am from '\' ?>
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"; }
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;
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定义的常量是从属于其命名空间的,在其余命名空间访问须要加命名空间的限定