2011-10-07 89 views
0

我有一個spl_autoload被調用,但問題是第二個自動加載不執行,我不明白爲什麼。有了這段代碼,腳本就會死掉。我從文件夾數組中刪除類,自動加載將工作。我的代碼如下所示:spl_autoload不調用第二個自動加載函數

<?php 
ini_set('error_reporting', E_ALL); 
ini_set('display_errors','On'); 
/*** nullify any existing autoloads ***/ 
spl_autoload_register(null, false); 
/*** specify extensions that may be loaded ***/ 
spl_autoload_extensions('.php'); 

function dataLoader($class) { 
    foreach (array(PV_CORE.DS.'data'.DS, PV_CORE.DS.'system'.DS, PV_CORE.DS.'cms'.DS, PV_CORE.DS.'util'.DS,PV_CORE.DS.'components'.DS, PV_CORE.DS.'template'.DS) as $folder){ 
     if (is_file($folder.$class.'.php')) { 
      include_once $folder.$class.'.php'; 
     } 
    }//end foreach 
} 

function testLoader($class) { 
    die(); 
    $filename = $class. '.php'; 
    $file =PV_CORE.DS.'data'.DS.$filename; 
    if (!file_exists($file)) { 
     return false; 
    } 
    require_once $file; 
} 

spl_autoload_register('dataLoader'); 
spl_autoload_register('testLoader'); 
+0

你的功能是登記:print_r的(spl_autoload_functions());我沒有真正明白你想做什麼。 – Talisin

回答

1

您的代碼有效,但可能是誤解。

你的功能登錄:

print_r(spl_autoload_functions()); 

回報:

Array 
(
    [0] => dataLoader 
    [1] => testLoader 
) 

,如果你初始化類

$class_obj = new ClassName(); 

的DataLoader會試圖加載文件:

$folder.ClassName.php 

如果您的腳本首先找不到該類,您的腳本將只加載第二個或任何其他註冊的函數。

因此,如果您在函數dataLoader __autoload中刪除您的$ class,將不會在第一個註冊函數中找到該類,因此他會嘗試在第二個註冊函數中查找它,依此類推。

+0

是的,我認爲spl_autoload加載所有關閉,但它無法找到類時按需加載。 –

1

你需要

return true; //如果類已經加載並且你希望自動加載棧會被停止

return false; //如果類沒有加載,你要繼續自動加載堆棧

的執行您的回調中

希望這有助於

相關問題