Birlikisgu Birlikisgu - 1 year ago 123
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.

Answer Source

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;

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download