PHP-命名空间
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了PHP-命名空间,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3277字,纯文字阅读大概需要5分钟。
内容图文
1.命名空间的意义
假设有下面两个文件:
include.php
1 <?php 2function test(){ 34 }; 5 ?>
test.php
1 <?php 2include "include.php"; 34function test(){ 56 }; 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 <?php 2 namespace includespace; 3function test(){ 4echo "i am from includespace <br>"; 5 } 6 ?>
test.php
1 <?php 2 namespace testspace; 3 4include "include.php"; 5function test(){ 6echo "i am from testspace <br>"; 7 } 8 9 test();//输出i am from testspace10 \includespace\test();//输出i am from includespace11 ?>
没有设置命名空间的都同属于公共空间‘\‘。例如上一个例子,如果不给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 ‘\‘ ?>
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; 3function test(){ 4echo "grandfa"; 5 } 6 7$son1 = new father\son\son; 8/***************************************/ 9 namespace grandfa\father; 10function test(){ 11echo "father"; 12 } 131415/**************************************/16 namespace grandfa\father\son; 17function test(){ 18echo "son"; 19 } 2021class son{}; 2223 ?>
如果我们想在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; 3define(‘DEA‘,‘1111‘); 4define(‘DEB‘,‘2222‘); 5const A=‘1111‘; 6const B=‘2222‘; 7 8echo A;echo B; 9echo DEA;echo DEB; 10/***************************************/11 namespace grandfa\father; 12function test(){ 13echo "father"; 14 } 1516echo \grandfa\A;echo \grandfa\B; 17echo DEA;echo DEB; 18/**************************************/19 namespace grandfa\father\son; 20function test(){ 21echo "son"; 22 } 2324class son{}; 2526echo \grandfa\A;echo \grandfa\B; 27echo DEA;echo DEB; 28 ?>
define定义的常量是全局的任何命名空间都可以访问,而const定义的常量是从属于其命名空间的,在其他命名空间访问需要加命名空间的限定
原文:https://www.cnblogs.com/patermenkey/p/8311597.html
内容总结
以上是互联网集市为您收集整理的PHP-命名空间全部内容,希望文章能够帮你解决PHP-命名空间所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。