如何自动包含?

Bir*_*sgu 0 php oop

我有autoload.php.这是为自动加载我的类而运行的.我想在主文件夹中包含autoload.php作为自动我的所有php文件.

喜欢:

include_to("index.php, example.php, ............... , other.php");
Run Code Online (Sandbox Code Playgroud)

同时我知道php的运行风格.

ter*_*ško 7

你做错了.

相反,您应该使用spl_autoload_register()创建自动加载器,加载文件.如果它找不到你使用的课程.

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;

});
Run Code Online (Sandbox Code Playgroud)