PHP最佳实践——自动加载类
2018-09-13 本文已影响0人
ozil_oo
使用[spl_autoload_register()](http://php.net/manual/en/function.spl-autoload-register.php)
来注册你的自动加载函数
<?php
// First, define your auto-load function.
function MyAutoload($className){
include_once $className . '.php';
}
// Next, register it with PHP.
spl_autoload_register('MyAutoload');
// Try it out!
// Since we haven't included a file defining the MyClass object, our auto-loader will kick in and include MyClass.php.
// For this example, assume the MyClass class is defined in the MyClass.php file.
$var = new MyClass();
?>