PHP spl_autoload_register 多次调用

2014-07-03 13:38:42

    spl_autoload_register 是注册php auto_load的函数,这个函数可以多次加载

    每一个函数应该都有返回值(boolean),如果返回值为true则认为已经加载成功就退出了加载过程,如果失败则继续调用后边的auto_load函数加载php文件,当然如果最后一个auto_load也没有加载成功这时候就没有加载完成,new的时候就会报错。

<?php
/**
 * Created by PhpStorm.
 * User: chengtao
 * Date: 14-7-3
 * Time: 上午11:27
 */


define('BASE_PATH',dirname(__FILE__).'/') ;

function cron_autoload1 ($name) {
    $file = BASE_PATH.'lib1/'.$name.'.class.php';
    if(file_exists($file)){
        include_once($file);
        return true;
    }

}
function cron_autoload2 ($name) {
    $file = BASE_PATH.'lib2/'.$name.'.class.php';
    if(file_exists($file)){
        include_once($file);
        return true;
    }
}
spl_autoload_register('cron_autoload1');
spl_autoload_register('cron_autoload2');

new Class1();
new Class2();

wKioL1O077KQSDTBAAD4JwxJpds988.jpg

https://blog.51cto.com/chengtao1633/1433946

猜你喜欢

转载自blog.csdn.net/m0_37477061/article/details/88391986