php - 使用php自动加载多个文件夹中的多个文件?

标签 php autoload

如何自动加载可能存储在多个文件夹中的多个文件? 我已经做的就像图片所示: enter image description here

创建 Index.php [ init.php ] 所需的文件,其中包含:

<?php
spl_autoload_register(function ($class){
    require_once "classes/class.$class.php";
});

问题1:如何自动加载位于另一个文件夹中的另一个文件,例如:Conf/class.Conf.php问题 2:我可以为其他自动加载过程使用其他名称约定吗?

如果您提供一个编码示例会更好:)

最佳答案

问题已解决,@dan-miller 评论提到检查文件是否存在,然后为每个新文件夹要求它

<?php
spl_autoload_register(function ($class){
    $filename="classes/$class.php";
        if(!file_exists($filename))
        {
            return "file : $filename is not Exist on the Given Path";
        }
    require_once "classes/$class.php";
});
spl_autoload_register(function ($class){
    $filename="conf/$class.php";
        if(!file_exists($filename))
        {
            return "file : $filename is not Exist on the Given Path";
        }
    require_once "conf/$class.php";
});

*** 这是为了测试目的

关于php - 使用php自动加载多个文件夹中的多个文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44023534/

相关文章:

php - 从子对象调用对象方法(PHP)

php - 如何使用查询结果来确定 DIV 的类?

php - 关于一个简单的 PHP Autoloader 的问题

PHP OOP 自动加载类命名空间问题

php - 如何向 CodeIgniter 添加自动加载功能?

perl - Perl 中 AUTOLOAD 的默认参数是什么?

php - 如何在 foreach 查询中插入第二个 foreach 查询?

php - 相同的 mysql 查询在不同时间在 phpmyadmin 中给出不同的结果

php - Composer 依赖冲突

PHP 类自动加载