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