开发者

spl_autoload not calling second autoload function

开发者 https://www.devze.com 2023-04-11 03:12 出处:网络
I have an spl_autoload being called but the problem is the second autoload does not execute and I can\'t figure out why. With this code this code the script should die. I remove classes from folder ar

I have an spl_autoload being called but the problem is the second autoload does not execute and I can't figure out why. With this code this code the script should die. I remove classes from folder array, the autoload will work. My code looks like this:

<?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'.D开发者_开发百科S, 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');


Your code works but probably a misunderstanding.

Your functions are registered:

print_r( spl_autoload_functions() );

returns:

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

and if you initialize a class

$class_obj = new ClassName();

dataLoader will try to load the file:

$folder.ClassName.php

Your script will only load the second or any other registered function if he can't find the class in the first place.

So if you remove your $class in the function dataLoader __autoload wont find the class anymore in the first registered function so he will try to look up for it in the second registered function and so on.


you need to

return true; // if the class has loaded and you want that the autoload stack will be stopped

return false; // if the class has not loaded and you want to continue the execution of the autoload stack

inside your callbacks

hope this helps

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号