提示

要寫出能經(jīng)受住時間考驗的代碼,建議在全局的命名空間中,盡量不要用變量、函數(shù)或類名,以避免與其它用戶空間代碼出現(xiàn)命名空間沖突。

防止函數(shù)和類的命名沖突的一個常見方法是使用自己專屬名字的 namespace

<?php

namespace MyProject;

function 
my_function() {
    return 
true;
}

\
MyProject\my_function();

這仍然需要你跟蹤已經(jīng)使用過的命名空間,但一旦你用了在決定了要使用的命名空間后,你可以添加所有的函數(shù)和類到它,不用再考慮沖突。

最好的做法是,盡量控制一下添加到全局范圍內(nèi)的變量,以防止與第三方代碼的命名產(chǎn)生命名空間沖突。

注意: Variable scoping

Because of PHP's scoping rules variables defined inside functions and methods are not in the global scope and as such cannot conflict with other variables defined in the global scope.