2011-10-07 86 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; //如果类没有加载,你要继续自动加载堆栈

的执行您的回调中

希望这有助于

相关问题