Birlikisgu Birlikisgu - 23 days ago 5x
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.


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

Meantime I know php's running style.


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;