两者都是php实现懒加载的函数,但__autoload官方已经不推荐(php7会直接抛出异常),区别是__autoload会自动执行,而spl_autoload_register()手动定义。
1.新建mysql.php类文件
class mysql{
function doPrint(){
echo 'hello mysql';
}
}
2.新建一个index.php文件,调用类的doPrint方法
一.autoload方式
function __autoload($className){
$file = $className. '.php';
if (is_file($file)) {
require_once($file);
}
}
二.spl_autoload_register方式
function autoload($className) {
$file = $className. '.php';
if (is_file($file)) {
require_once($file);
}
}
spl_autoload_register('autoload');
3.index.php里执行
$obj = new mysql();
$obj->doPrint();