php自动加载机制的实现

一、什么是自动加载

自动加载就是当我们在当前文件中实例化一个不存在的类时,调用自动加载机制引入相应的类文件。

注:自动加载有两种方式(都是php内置的),一种是通过__autoload(),另一种是通过spl_autoload_register()。

以下两种方式的介绍中,都是执行test3.php文件。

二、通过__autoload() 实现自动加载

/data/www/test2/test2.php
<?php

class test2
{

    function aa()
    {
        echo 'this is function aa';
        echo "<br><br>";
    }

    static function bb()
    {
        echo 'this is function bb';
        echo "<br><br>";
    }
}

/data/www/test3.php

<?php

//加载过程
//1、实例化test2类,由于当前文件不存在test2类,所以会自动执行__autoload()方法,并自动将类名传过去
//2、执行完__autoload()方法之后,会加载test2/test2.php文件
//3、由于当前文件已经通过__autoload()方式require进文件test2.php了,所以也就可以调用test2.php中的方法了

$test = new test2();
$test->aa();//调用aa方法
test2::bb();//调用bb静态类方法

function __autoload($class)
{
    echo '当前自动加载的类名为'.$class."<br><br>";//此时,$class的值就是test2

    include_once __DIR__.'/test2/'.$class.'.php';//相当于加载了test2/test2.php
}

三、通过spl_autoload_register()实现自动加载【推荐的方式】

注:由于__autoload()自动加载方式已经逐渐被php官方废弃了,所以这里采用另一种方式spl_autoload_register来实现。

这里,test2.php文件和上面一样,只改变test3.php文件。

/data/www/test3.php
<?php

//加载过程
//1、实例化test2类时,由于当前文件并没有test2类,所以php会自动调用spl_autoload_register自动加载机制
//2、调用spl_autoload_register,就会调用我们自己定义的autoload_test()方法
//3、进而引入了相应的test2.php类文件
//4、引入了相应的类文件之后,自然也就可以实例化类,并调用相应的方法了

spl_autoload_register(autoload_test);

$test = new test2();
$test->aa();//调用aa方法
test2::bb();//调用bb静态类方法

/**
 * 用于实现自动加载
 * @param $class
 */
function autoload_test($class)
{
    echo '当前自动加载的类名为'.$class."<br><br>";//此时,$class的值就是test2

    include_once __DIR__.'/test2/'.$class.'.php';//相当于加载了test2/test2.php
}

四、总结

spl_autoload_register()相比于__autoload()的优点在于:

(1)可以按需多次写spl_autoload_register注册加载函数,加载顺序按谁先注册谁先调用。__aotuload由于是全局函数只能定义一次,不够灵活。

比如下面,由于需要同时加载test2.php 以及 test4.class.php,__autoload就实现不了这个需求,而使用spl_autoload_register来实现就比较合适。

test4.class.php
<?php

class test4
{

    function dd()
    {
        echo "this is test4 function dd";
    }

}
test3.php
<?php

//加载过程
//1、实例化test2类时,由于当前文件并没有test2类,所以php会自动调用spl_autoload_register自动加载机制
//2、调用spl_autoload_register,就会调用我们自己定义的autoload_test()方法
//3、进而引入了相应的test2.php类文件
//4、之后又实例化了test4类,test4类在当前文件同样没有,这时php会自动调用spl_autoload_register自动加载机制
//4.1 首先调用第一个注册自动加载函数spl_autoload_register(autoload_test),加载之后没有找到test4类
//4.2 所以php会继续调用第二个自动注册函数spl_autoload_register(autoload_test4)
//4.3 这时,终于找到test4类了,也就不用继续在往下找了
//5、引入了相应的类文件之后,自然也就可以实例化类,并调用相应的方法了

spl_autoload_register(autoload_test);
spl_autoload_register(autoload_test4);

$test = new test2();
$test->aa();//调用aa方法
test2::bb();//调用bb静态类方法

$test4 = new test4();
$test4->dd();


/**
 * 用于实现自动加载
 * @param $class
 */
function autoload_test($class)
{
    echo '当前自动加载的类名为'.$class."<br><br>";//此时,$class的值就是test2
    include_once __DIR__.'/test2/'.$class.'.php';//相当于加载了test2/test2.php
}

function autoload_test4($class)
{
    echo '当前自动加载的类名为'.$class."<br><br>";//此时,$class的值就是test4
    include_once __DIR__.'/'.$class.'.class.php';//相当于加载了test4.class.php
}

(2)spl_autoload_register()可以被catch到错误,而__aotuload不能。

(3)spl_autoload_register注册的加载函数可以按需被spl_autoload_unregister掉

还有,值得注意的是,如果对类文件加入了命名空间,就必须保证正确的加载了类文件的同时,还要通过use引入对应的命名空间。