php - spl_autoload_register()怎样注册多个自动加载函数?

查看:163
本文介绍了php - spl_autoload_register()怎样注册多个自动加载函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

<?php 

/*function __autoload($class){
    require("./class/".$class.".php");
}*/

function auto($class){
    require("./class/".$class.".php");
}
function aa($class){
    require("./class2/".$class.".php");
}

spl_autoload_register("auto");
spl_autoload_register("aa");

$cc=spl_autoload_functions();
var_dump($cc);

$a=new c();
$a->index();




 ?>

输出:

array(2) { [0]=> string(4) "auto" [1]=> string(2) "aa" } 
Warning: require(./class/c.php): failed to open stream: No such file or directory in D:\www\a\auto.php on line 8

Fatal error: require(): Failed opening required './class/c.php' (include_path='.;C:\php\pear') in D:\www\a\auto.php on line 8

文件:

解决方案

require之前要先判断文件是否存在哦

function auto($class){
    if(file_exists("./class/".$class.".php")) {
        require("./class/".$class.".php");
    }
}
function aa($class){
    if(file_exists("./class2/".$class.".php")) {
        require("./class2/".$class.".php");
    }
}

这篇关于php - spl_autoload_register()怎样注册多个自动加载函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆