2016-04-07 34 views
0

我正在使用php命名空間和自動加載。作爲每個頁面的頂部結果我寫這幾行:PHP命名空間在子目錄中不起作用

require_once('autoload.php'); // this file is in the root directory 
use Lib\Blogs; 
use Lib\Clients; 

每一件事情是罰款與上述各行,當我在網站的根目錄。但我不能訪問它們中的任何時候我在子目錄:

我上面的行改成這樣:

require_once('../autoload.php'); // this file is in the root directory 
use Lib\Blogs; 
use Lib\Clients; 

,這是錯誤:

Fatal error: Class 'Lib\Blogs' not found in C:\website\ajax\ajaxBlog.php on line 10 

和在第10行,我有這樣的代碼,調用靜態方法:

if (!empty(Blogs::findByEmail($email))) { ... } 

回答

0

所以,你必須使用類與別名或寫福當你打電話時,你會得到一條路

use Lib\Blogs as Blogs; 
... 
if (!empty(Blogs::findByEmail($email))) { ... } 

if (!empty(Lib\Blogs::findByEmail($email))) { ... } 
+0

還是一樣的致命錯誤 – MoHo

+0

我還需要autoload.php文件? – MoHo

+0

當然,您需要在每個頁面上使用自動更正器。否則,您必須手動包含所有類文件。在你的情況下,你必須包含絕對路徑的自動加載器文件。但是如果你在你的項目中使用MVC模式會更好。在這種情況下,你只需要在index.php中包含一次自動加載器 –

相關問題