PHP命名空間

這幾天沒更,是由於上一篇翻譯太長了,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
?>
相關文章
相關標籤/搜索