Birlikisgu Birlikisgu - 2 months ago 19
PHP Question

How to auto include?

I have autoload.php . This is running for auto loading my classes. I want to include autoload.php as auto my all php files in main folder.

Like:

include_to("index.php, example.php, ............... , other.php");


Meantime I know php's running style.

Answer

You are doing it wrong.

Instead you should be using spl_autoload_register() to create an autoloader, which load file. if it cannot find the class you use.

spl_autoload_register(function( $className ){

    $filepath = '/path/to/files/' . strtolower( $className ) . '.php';

    if ( !file_exists( $filepath ) )
    {
        $message  = "Cannot load class: $className. ";
        $message .= "File '$filepath' not found!";
        throw new Exception( $message ); 
    }

    require $filepath;

});