Sergio Flores
Sergio Flores

Reputation: 5427

Autoload files when use require function

There's a magic function to autoload classes (__autoload), I want to know if there a way to load a file without a class.

Something like this:

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.");

}

Upvotes: 0

Views: 619

Answers (1)

Jakub Truneček
Jakub Truneček

Reputation: 8990

You can define own function for requiring:

function require_file($file) {
    // your code
}

and then call it

require_file('file');

I guess that there is no way to overload require function.

Upvotes: 1

Related Questions