spl_autoload没有调用第二个自动加载功能

时间:2011-10-07 04:36:13

标签: php spl

我有一个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');

2 个答案:

答案 0 :(得分:1)

你的代码有效但可能是一种误解。

您的功能已注册:

print_r( spl_autoload_functions() );

返回:

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

如果你初始化一个类

$class_obj = new ClassName();

dataLoader将尝试加载文件:

$folder.ClassName.php

如果您的脚本首先找不到该类,则只会加载第二个或任何其他已注册的函数。

因此,如果您在函数dataLoader __autoload中删除$ class,则不会在第一个注册函数中找到该类,因此他将尝试在第二个注册函数中查找它,依此类推。

答案 1 :(得分:1)

你需要

返回true; //如果类已加载并且您希望自动加载堆栈将被停止

返回false; //如果该类尚未加载并且您想继续执行自动加载堆栈

回调中的

希望这会有所帮助