2016-02-19 85 views
0

我在我的項目目錄結構如下:如何在不帶參數的情況下使用spl_autoload_register?

Test 
--Cats 
----Fatty.php // namespace: Test\Cats 
--index.php 

的index.php:

spl_autoload_extensions(".php"); 
spl_autoload_register(); 

Test\Cats\Fatty::fattyMeow(); 

我得到的錯誤:

Fatal error: Class 'Test\Cats\Fatty' not found in /var/www/public/Test/index.php on line 9

this在PHP評論.net,spl_autoload_register()應該工作得很好沒有任何autoload功能提供如果名稱空間匹配直接項目的結構。那麼,爲什麼上面的代碼不起作用?還是我誤解了某些東西?

+0

什麼是你所得到的錯誤? –

+1

我沒有看到Techniques文件夾也沒有看到Grumpy.php –

+0

對不起,爲了簡化結構,我修改了一下 –

回答

1

這不適合你的原因是因爲你的名字空間中包含主目錄Test的名字。當你這樣做,你運行spl_autoload它會認爲該目錄作爲兄弟index.php存在。

爲了解決這個問題調整自己的命名空間,並刪除Test

Cats\Fatty::fattyMeow(); 
在Fatty.php

的index.php中

namespace Cats; 
+0

感謝您的答案。不幸的是,錯誤仍然存​​在。 –

+0

您是否收到相同的錯誤或不同? – CodeGodie

+0

對不起,我拼錯方法名稱。你的解決方案就像一個魅力! –

相關問題