PHP 5.3 的一个新的重要特性就是 命名空间(namespace)。
这一特性在 PHP5.0x 时候就提出过,后来被取消并安排在 PHP6 中实现。而此次又再次“提前”到了 PHP 5.3 发布,可见开发人员对其的重视以及谨慎的态度。
官方发布时说明文档的内容可能已过期(documentation maybe out dated),所以在这里简单的说明命名空间的用法:首先是声明一个命名空间,加入了新的关键字 namespace ,其应在类文件的开头
12345678 Copy after login | <SPAN style="FONT-WEIGHT: bold; COLOR: rgb(0,0,0)"><?php</SPAN> namespace Project<SPAN style="COLOR: rgb(51,153,51)">::</SPAN><SPAN style="COLOR: rgb(0,64,0)">Module</SPAN><SPAN style="COLOR: rgb(51,153,51)">;</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: rgb(0,0,0)">class</SPAN> User <SPAN style="COLOR: rgb(0,153,0)">{</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: rgb(0,0,0)">const</SPAN> STATUS_OK <SPAN style="COLOR: rgb(51,153,51)">=</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: rgb(0,0,0)">true</SPAN><SPAN style="COLOR: rgb(51,153,51)">;</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: rgb(0,0,0)">function</SPAN> register<SPAN style="COLOR: rgb(0,153,0)">(</SPAN><SPAN style="COLOR: rgb(0,0,136)">$data</SPAN><SPAN style="COLOR: rgb(0,153,0)">)</SPAN> <SPAN style="COLOR: rgb(0,153,0)">{</SPAN> <SPAN style="COLOR: rgb(51,153,51)">...</SPAN> <SPAN style="COLOR: rgb(0,153,0)">}</SPAN> <SPAN style="COLOR: rgb(51,153,51)">...</SPAN> <SPAN style="COLOR: rgb(0,153,0)">}</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: rgb(0,0,0)">?></SPAN> Copy after login |
然后在控制器中(可能是其他文件)就可以这样调用
12 Copy after login Copy after login Copy after login | <SPAN style="COLOR: rgb(0,0,136)">$user</SPAN> <SPAN style="COLOR: rgb(51,153,51)">=</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: rgb(0,0,0)">new</SPAN> Project<SPAN style="COLOR: rgb(51,153,51)">::</SPAN><SPAN style="COLOR: rgb(0,64,0)">Module</SPAN><SPAN style="COLOR: rgb(51,153,51)">::</SPAN><SPAN style="COLOR: rgb(0,64,0)">User</SPAN><SPAN style="COLOR: rgb(0,153,0)">(</SPAN><SPAN style="COLOR: rgb(0,153,0)">)</SPAN><SPAN style="COLOR: rgb(51,153,51)">;</SPAN> <SPAN style="COLOR: rgb(0,0,136)">$user</SPAN><SPAN style="COLOR: rgb(51,153,51)">-></SPAN><SPAN style="COLOR: rgb(0,64,0)">register</SPAN><SPAN style="COLOR: rgb(0,153,0)">(</SPAN><SPAN style="COLOR: rgb(0,0,136)">$register_info</SPAN><SPAN style="COLOR: rgb(0,153,0)">)</SPAN><SPAN style="COLOR: rgb(51,153,51)">;</SPAN> Copy after login |
的确与平常的并无两样,但是我们可以将两个相互独立的类联系起来。比如
12 Copy after login Copy after login Copy after login | Project<SPAN style="COLOR: rgb(51,153,51)">::</SPAN><SPAN style="COLOR: rgb(0,64,0)">Module</SPAN><SPAN style="COLOR: rgb(51,153,51)">::</SPAN><SPAN style="COLOR: rgb(0,64,0)">User</SPAN><SPAN style="COLOR: rgb(51,153,51)">;</SPAN> Project<SPAN style="COLOR: rgb(51,153,51)">::</SPAN><SPAN style="COLOR: rgb(0,64,0)">Module</SPAN><SPAN style="COLOR: rgb(51,153,51)">::</SPAN><SPAN style="COLOR: rgb(0,64,0)">Blog</SPAN><SPAN style="COLOR: rgb(51,153,51)">;</SPAN> Copy after login |
这样就能从语言本身更容易描述和理解变量、类之间的关系,从而避免了“传统”上的 Project_Module_Blog 这样冗长的命名方式。
上面的说明可能很难说明使用命名空间带来了什么好处,新增加的 use 和 as 关键字或许能更好的说明问题。use 和 as 语句可以引用和声明 命名空间的“别名”。比如,上述的控制器中实例化类的代码可以这样写
123 Copy after login Copy after login Copy after login | use Project<SPAN style="COLOR: rgb(51,153,51)">::</SPAN><SPAN style="COLOR: rgb(0,64,0)">Module</SPAN><SPAN style="COLOR: rgb(51,153,51)">;</SPAN> <SPAN style="COLOR: rgb(0,0,136)">$user</SPAN> <SPAN style="COLOR: rgb(51,153,51)">=</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: rgb(0,0,0)">new</SPAN> Module<SPAN style="COLOR: rgb(51,153,51)">::</SPAN><SPAN style="COLOR: rgb(0,64,0)">User</SPAN><SPAN style="COLOR: rgb(0,153,0)">(</SPAN><SPAN style="COLOR: rgb(0,153,0)">)</SPAN><SPAN style="COLOR: rgb(51,153,51)">;</SPAN> <SPAN style="COLOR: rgb(0,0,136)">$user</SPAN><SPAN style="COLOR: rgb(51,153,51)">-></SPAN><SPAN style="COLOR: rgb(0,64,0)">register</SPAN><SPAN style="COLOR: rgb(0,153,0)">(</SPAN><SPAN style="COLOR: rgb(0,0,136)">$register_info</SPAN><SPAN style="COLOR: rgb(0,153,0)">)</SPAN><SPAN style="COLOR: rgb(51,153,51)">;</SPAN> Copy after login |
甚至
123 Copy after login Copy after login Copy after login | use Project<SPAN style="COLOR: rgb(51,153,51)">::</SPAN><SPAN style="COLOR: rgb(0,64,0)">Module</SPAN><SPAN style="COLOR: rgb(51,153,51)">::</SPAN><SPAN style="COLOR: rgb(0,64,0)">User</SPAN> <SPAN style="COLOR: rgb(177,177,0)">as</SPAN> ModuleUser<SPAN style="COLOR: rgb(51,153,51)">;</SPAN> <SPAN style="COLOR: rgb(0,0,136)">$user</SPAN> <SPAN style="COLOR: rgb(51,153,51)">=</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: rgb(0,0,0)">new</SPAN> ModuleUser<SPAN style="COLOR: rgb(51,153,51)">;</SPAN> <SPAN style="COLOR: rgb(0,0,136)">$user</SPAN><SPAN style="COLOR: rgb(51,153,51)">-></SPAN><SPAN style="COLOR: rgb(0,64,0)">register</SPAN><SPAN style="COLOR: rgb(0,153,0)">(</SPAN><SPAN style="COLOR: rgb(0,0,136)">$register_info</SPAN><SPAN style="COLOR: rgb(0,153,0)">)</SPAN><SPAN style="COLOR: rgb(51,153,51)">;</SPAN> Copy after login |
类中的常量也可以通过命名空间访问,比如上述类中的 STATUS_OK 就可以通过命名空间
1 Copy after login Copy after login | Project<SPAN style="COLOR: rgb(51,153,51)">::</SPAN><SPAN style="COLOR: rgb(0,64,0)">Module</SPAN><SPAN style="COLOR: rgb(51,153,51)">::</SPAN><SPAN style="COLOR: rgb(0,64,0)">User</SPAN><SPAN style="COLOR: rgb(51,153,51)">::</SPAN><SPAN style="COLOR: rgb(0,64,0)">STATUS_OK</SPAN> Copy after login |
访问。进一步的,也可以用别名简化那么长的“变量名称”
12 Copy after login Copy after login Copy after login | use Project<SPAN style="COLOR: rgb(51,153,51)">::</SPAN><SPAN style="COLOR: rgb(0,64,0)">Module</SPAN><SPAN style="COLOR: rgb(51,153,51)">::</SPAN><SPAN style="COLOR: rgb(0,64,0)">User</SPAN><SPAN style="COLOR: rgb(51,153,51)">::</SPAN><SPAN style="COLOR: rgb(0,64,0)">STATUS_OK</SPAN> as STATUS_OK; echo STATUS_OK; Copy after login |
顺便提下“超空间(The Global Namespace)”的概念。所谓的“超空间”,就是没有指定命名空间的变量、类和函数。比如
123 Copy after login Copy after login Copy after login | <SPAN style="FONT-WEIGHT: bold; COLOR: rgb(0,0,0)">function</SPAN> foo<SPAN style="COLOR: rgb(0,153,0)">(</SPAN><SPAN style="COLOR: rgb(0,153,0)">)</SPAN> <SPAN style="COLOR: rgb(0,153,0)">{</SPAN> <SPAN style="COLOR: rgb(51,153,51)">...</SPAN> <SPAN style="COLOR: rgb(0,153,0)">}</SPAN> Copy after login |
这的函数,可以使用 foo() 执行的同时,也可以使用 ::foo(); 这样执行。
最后,配合使用 autoload 函数即可载入指定命名空间的类。简单的函数如下
12345 Copy after login | <SPAN style="FONT-WEIGHT: bold; COLOR: rgb(0,0,0)">function</SPAN> __autoload<SPAN style="COLOR: rgb(0,153,0)">(</SPAN> <SPAN style="COLOR: rgb(0,0,136)">$classname</SPAN> <SPAN style="COLOR: rgb(0,153,0)">)</SPAN> <SPAN style="COLOR: rgb(0,153,0)">{</SPAN> <SPAN style="COLOR: rgb(0,0,136)">$classname</SPAN> <SPAN style="COLOR: rgb(51,153,51)">=</SPAN> <SPAN style="COLOR: rgb(153,0,0)">strtolower</SPAN><SPAN style="COLOR: rgb(0,153,0)">(</SPAN> <SPAN style="COLOR: rgb(0,0,136)">$classname</SPAN> <SPAN style="COLOR: rgb(0,153,0)">)</SPAN><SPAN style="COLOR: rgb(51,153,51)">;</SPAN> <SPAN style="COLOR: rgb(0,0,136)">$classname</SPAN> <SPAN style="COLOR: rgb(51,153,51)">=</SPAN> <SPAN style="COLOR: rgb(153,0,0)">str_replace</SPAN><SPAN style="COLOR: rgb(0,153,0)">(</SPAN> <SPAN style="COLOR: rgb(0,0,255)">'::'</SPAN><SPAN style="COLOR: rgb(51,153,51)">,</SPAN> DIRECTORY_SEPARATOR<SPAN style="COLOR: rgb(51,153,51)">,</SPAN> <SPAN style="COLOR: rgb(0,0,136)">$classname</SPAN> <SPAN style="COLOR: rgb(0,153,0)">)</SPAN><SPAN style="COLOR: rgb(51,153,51)">;</SPAN> <SPAN style="COLOR: rgb(177,177,0)">require_once</SPAN><SPAN style="COLOR: rgb(0,153,0)">(</SPAN> <SPAN style="COLOR: rgb(153,0,0)">dirname</SPAN><SPAN style="COLOR: rgb(0,153,0)">(</SPAN> <SPAN style="FONT-WEIGHT: bold; COLOR: rgb(0,0,0)">__FILE__</SPAN> <SPAN style="COLOR: rgb(0,153,0)">)</SPAN> <SPAN style="COLOR: rgb(51,153,51)">.</SPAN> <SPAN style="COLOR: rgb(0,0,255)">'/'</SPAN> <SPAN style="COLOR: rgb(51,153,51)">.</SPAN> <SPAN style="COLOR: rgb(0,0,136)">$classname</SPAN> <SPAN style="COLOR: rgb(51,153,51)">.</SPAN> <SPAN style="COLOR: rgb(0,0,255)">'.class.php'</SPAN> <SPAN style="COLOR: rgb(0,153,0)">)</SPAN><SPAN style="COLOR: rgb(51,153,51)">;</SPAN> <SPAN style="COLOR: rgb(0,153,0)">}</SPAN> Copy after login |
这样,比如调用
1 Copy after login Copy after login | __autoload<SPAN style="COLOR: rgb(0,153,0)">(</SPAN><SPAN style="COLOR: rgb(0,0,255)">'Project::Module::User'</SPAN><SPAN style="COLOR: rgb(0,153,0)">)</SPAN><SPAN style="COLOR: rgb(51,153,51)">;</SPAN> Copy after login |
就可以自动载入 Project_Module_User.class.php 文件