如何要求一次所有的php文件?

如何要求一次所有的php文件?,php,Php,我厌倦了在我的每个php文件中一次又一次地使用require,这是一种在所有文件夹中自动获取require所有需要的php文件的方法 我用过这个,但只适用于一个文件夹 spl_autoload_register(function($class){ require_once 'logic/'. $class .'.php'; }); 请帮助我通过减少需求来改进我的OOPS php。您也可以通过这种方式使用,您可以为要搜索的文件定义多个路径 spl_autoload_register(a

我厌倦了在我的每个php文件中一次又一次地使用require,这是一种在所有文件夹中自动获取require所有需要的php文件的方法

我用过这个,但只适用于一个文件夹

spl_autoload_register(function($class){
    require_once 'logic/'. $class .'.php';
}); 

请帮助我通过减少需求来改进我的OOPS php。

您也可以通过这种方式使用,您可以为要搜索的文件定义多个路径

spl_autoload_register(array("Loader","classLoader"));
class Loader
{
    public static function classLoader($className)
    {
        if(!empty($className))
        {
            foreach(array(
                "path/to/path1/",
                "path/to/path2/",
                "path/to/path3/"
            ) as $path)
            {
                $classPath=$path.$className.".php";
                if(file_exists($classPath))
                {
                    require_once $classPath;
                }
            }
        }
    }
}

您可以创建一个“主require文件”,其中包含您需要的文件,然后在所有页面上都需要该文件。或者在php.ini中使用
auto_prepend_file
。如果您使用类,您可以使用auto_prepend_file的工作方式。我使用文件夹和子文件夹。这很好,但我希望所有的php文件都没有calling@NkhangweniMulelu为此,您可以做一件事,那就是列出要包含的所有文件。只需提及上述代码数组中的所有文件。并删除我在其中附加.php的那一行