这几天没更,是由于上一篇翻译太长了,233333,其实并无间断,今天看了下啊PHP命名空间,以下:php
【参考】PHP命名空间(Namespace)的使用详解,官方文档,菜鸟教程html
命名空间一个最明确的目的就是解决重名问题,PHP中不容许两个函数或者类出现相同的名字,不然会产生一个致命的错误。函数
1. 约定一个前缀。post
例:项目中有两个模块:article和message board,它们各自有一个处理用户留言的类Comment。以后我可能想要增长对全部用户留言的一些信息统计功能,好比说我想获得全部留言的数量。这时候调用它们Comment提供的方法是很好的作法,可是同时引入各自的Comment类显然是不行的,代码会出错,在另外一个地方重写任何一个Comment也会下降维护性。那这时只能重构类名,我约定了一个命名规则,在类名前面加上模块名,像这样:Article_Comment、MessageBoard_Commenturl
能够看到,名字变得很长,那意味着之后使用Comment的时候会写上更多的代码(至少字符多了)。而且,之后若是要对各个模块增长更多的一些整合功能,或者是互相调用,发生重名的时候就须要重构名字。固然在项目开始的时候就注意到这个问题,并规定命名规则就能很好的避免这个问题。spa
2. 使用命名空间。.net
建立一个命名空间须要使用namespace关键字,要注意的是,当前脚本文件的第一个命名空间前面不能有任何代码以下是错误的:翻译
<html></html>
<?php
namespace Article;
?>
在不一样空间之间不能够直接调用其它元素,须要使用命名空间的语法:code
<?php namespace Article; class Comment { } namespace MessageBoard; class Comment { } //调用当前空间(MessageBoard)的Comment类 $comment = new Comment(); //调用Article空间的Comment类 $article_comment = new \Article\Comment(); ?>
公共空间
我有一个common_inc.php脚本文件,里面有一些好用的函数和类:htm
<?php function getIP() { } class FilterXSS { } ?>
在一个命名空间里引入这个脚本,脚本里的元素不会归属到这个命名空间。若是这个脚本里没有定义其它命名空间,它的元素就始终处于公共空间中:
<?php namespace Blog\Article; //引入脚本文件 include './common_inc.php'; $filter_XSS = new FilterXSS(); //出现致命错误:找不到Blog\Article\FilterXSS类 $filter_XSS = new \FilterXSS(); //正确 ?>
调用公共空间的方式是直接在元素名称前加 \ 就能够了,不然PHP解析器会认为我想调用当前空间下的元素。除了自定义的元素,还包括PHP自带的元素,都属于公共空间。
要提一下,其实公共空间的函数和常量不用加 \ 也能够正常调用(不明白PHP为何要这样作),可是为了正确区分元素,仍是建议调用函数的时候加上 \
名称术语
1. 非限定名称,或不包含前缀的类名称,例如 $comment = new Comment();。若是当前命名空间是Blog\Article,Comment将被解析为Blog\Article\Comment。若是使用Comment的代码不包含在任何命名空间中的代码(全局空间中),则Comment会被解析为Comment。
2. 限定名称,或包含前缀的名称,例如 $comment = new Article\Comment();。若是当前的命名空间是Blog,则Comment会被解析为Blog\Article\Comment。若是使用Comment的代码不包含在任何命名空间中的代码(全局空间中),则Comment会被解析为Comment。
3. 彻底限定名称,或包含了全局前缀操做符的名称,例如 $comment = new \Article\Comment();。在这种状况下,Comment老是被解析为代码中的文字名(literal name)Article\Comment。
其实能够把这三种名称类比为文件名(例如 comment.php)、相对路径名(例如 ./article/comment.php)、绝对路径名(例如 /blog/article/comment.php)。
<?php //建立空间Blog namespace Blog; class Comment { } //非限定名称,表示当前Blog空间 //这个调用将被解析成 Blog\Comment(); $blog_comment = new Comment(); //限定名称,表示相对于Blog空间 //这个调用将被解析成 Blog\Article\Comment(); $article_comment = new Article\Comment(); //类前面没有反斜杆\ //彻底限定名称,表示绝对于Blog空间 //这个调用将被解析成 Blog\Comment(); $article_comment = new \Blog\Comment(); //类前面有反斜杆\ //彻底限定名称,表示绝对于Blog空间 //这个调用将被解析成 Blog\Article\Comment(); $article_comment = new \Blog\Article\Comment(); //类前面有反斜杆\ //建立Blog的子空间Article namespace Blog\Article; class Comment { } ?>
别名和导入
别名和导入能够看做是调用命名空间元素的一种快捷方式。PHP并不支持导入函数或常量。
它们都是经过使用use操做符来实现:
<?php namespace Blog\Article; class Comment { } //建立一个BBS空间(我有打算开个论坛) namespace BBS; //导入一个命名空间 use Blog\Article; //导入命名空间后可以使用限定名称调用元素 $article_comment = new Article\Comment(); //为命名空间使用别名 use Blog\Article as Arte; //使用别名代替空间名 $article_comment = new Arte\Comment(); //导入一个类 use Blog\Article\Comment; //导入类后可以使用非限定名称调用元素 $article_comment = new Comment(); //为类使用别名 use Blog\Article\Comment as Comt; //使用别名代替空间名 $article_comment = new Comt(); ?>
我注意到,若是导入元素的时候,当前空间有相同的名字元素将会怎样?显然结果会发生致命错误。
<?php namespace Blog\Article; class Comment { } namespace BBS; class Comment { } Class Comt { } //导入一个类 use Blog\Article\Comment; $article_comment = new Comment(); //与当前空间的Comment发生冲突,程序产生致命错误 //为类使用别名 use Blog\Article\Comment as Comt; $article_comment = new Comt(); //与当前空间的Comt发生冲突,程序产生致命错误 ?>
以下也是体现命名空间的引用方式:
file1.php 文件代码
//file1.php 文件代码 <?php namespace Foo\Bar\subnamespace; const FOO = 1; function foo() {} class foo { static function staticmethod() {} } ?>
file2.php 文件代码
<?php namespace Foo\Bar; include 'file1.php'; const FOO = 2; function foo() {} class foo { static function staticmethod() {} } /* 非限定名称 */ foo(); // 解析为 Foo\Bar\foo resolves to function Foo\Bar\foo foo::staticmethod(); // 解析为类 Foo\Bar\foo的静态方法staticmethod。resolves to class Foo\Bar\foo, method staticmethod echo FOO; // resolves to constant Foo\Bar\FOO /* 限定名称 */ subnamespace\foo(); // 解析为函数 Foo\Bar\subnamespace\foo subnamespace\foo::staticmethod(); // 解析为类 Foo\Bar\subnamespace\foo, // 以及类的方法 staticmethod echo subnamespace\FOO; // 解析为常量 Foo\Bar\subnamespace\FOO /* 彻底限定名称 */ \Foo\Bar\foo(); // 解析为函数 Foo\Bar\foo \Foo\Bar\foo::staticmethod(); // 解析为类 Foo\Bar\foo, 以及类的方法 staticmethod echo \Foo\Bar\FOO; // 解析为常量 Foo\Bar\FOO ?>