2010-07-06 62 views
8

我玩弄SPL自動加載功能,似乎缺少重要的東西,因爲我目前無法使其工作。這是我目前使用的片段:使用spl_autoload()無法加載類

// ROOT_DIRECTORY translates to /home/someuser/public_html/subdomains/test 
define('ROOT_DIRECTORY', realpath(dirname(__FILE__))); 
define('INCLUDE_DIRECTORY', ROOT_DIRECTORY . '/includes/classes/'); 
set_include_path(get_include_path() . PATH_SEPARATOR . INCLUDE_DIRECTORY); 
spl_autoload_extensions('.class.php, .interface.php, .abstract.php'); 
spl_autoload_register(); 

當我echo get_include_path()我得到我所期望的路徑:當我運行代碼,我得到這個錯誤信息

// Output echo get_include_path(); 
.:/usr/lib/php:/usr/local/lib/php:/home/someuser/public_html/subdomains/test/includes/classes/ 

但是:

Fatal error: spl_autoload() [function.spl-autoload]: Class Request could not be loaded in /home/someuser/public_html/subdomains/test/contact.php on line 5

Request.class.php爲絕對爲位於/ home/someuser/public_html/subdomains/test/includes/classes /目錄中。

我錯過了什麼?

+0

@Yannis Rizos - 這確實是我的問題。讓這個答案,所以我可以投票並給你正確的答案功勞。 – 2010-07-06 15:06:56

回答

19

http://www.php.net/manual/en/function.spl-autoload-register.php#96804有一個可能適用於您的問題的評論:spl_autoload_register()似乎不適合camelcase,在您的情況下可能會嘗試找到request.class.php而不是Request ...

+1

我也發現了這個錯誤。只要我將類名更改爲小寫,一切正常。你知道這是否被報告爲一個錯誤? – AntonioCS 2011-05-18 22:19:53

+2

有這方面的錯誤。可笑的是,這仍然是一個問題。我通過了bug系統並對相關的投票進行了投票,這裏是一個很好的開始:https://bugs.php.net/bug.php?id = 53065。 – zombat 2011-09-05 01:21:27

+1

如果你仍然想使用spl,你可以使用一個重載默認函數的閉包,所以你不會得到小寫:'spl_autoload_register(function($ class) { \t include $ class。'.php'; } );' - 調整你的路徑;) – 2014-10-10 10:44:27

0

你期望他們的路徑類應該是似乎不匹配的路徑。比較

.:/usr/lib/php:/usr/local/lib/php:/home/someuser/public_html/subdomains/test/includes/classes/ 

/home/someuser/public_html/subdomains/test/ 

不同的是,你們班是不是在includes/classes/爲您的SPL需要,但上述幾個目錄。

0

我收到了類似的錯誤消息,但我的問題是不同的。我的錯誤消息看起來像

PHP Fatal error: spl_autoload(): Class Lib\Lib\Regex could not be loaded in /dir1/dir2/lib/regex.php on line 49 

原來我忘了從Lib\Regex刪除Lib\ Regex類定義本身中。我有類似以下內容:

namespace Lib; 

class Regex { 

... 

    public static function match($pattern, $str) { 

     $regex = new Lib\Regex($pattern); 

     ... 
    } 
} 
相關問題