Php 使用require功能时自动加载文件

Php 使用require功能时自动加载文件,php,autoload,Php,Autoload,自动加载类()有一个神奇的功能,我想知道是否有办法在没有类的情况下加载文件 大概是这样的: require ('example_file'); // Trigger __autoloadfiles function function __autoloadfiles($filename) { $files = array( ROOT . DS . 'library' . DS . $filename. '.php', ROOT . DS . 'applic

自动加载类()有一个神奇的功能,我想知道是否有办法在没有类的情况下加载文件

大概是这样的:

require ('example_file'); // Trigger __autoloadfiles function

function __autoloadfiles($filename)
{
    $files = array(
        ROOT . DS . 'library' . DS . $filename. '.php',
        ROOT . DS . 'application' . DS . $filename . '.php',
        ROOT . DS . 'application/otherfolder' . DS . $filename. '.php'
    );

    $file_exists = FALSE;

    foreach($files as $file) {
        if( file_exists( $file ) ) {
            require_once $file;
            $file_exists = TRUE;
            break;
        }
    }

    if(!$file_exists)
        die("File not found.");

}

您可以定义自己的功能以满足以下要求:

function require_file($file) {
    // your code
}
然后叫它

require_file('file');
我想没有办法重载
require
函数