phper必知必会之类库自动加载的七种方式(三)

php自动加载


下面显示例子的文件目录结构图

一、没有使用命名空间的几种实现

test/oneClass.php

class oneClass{

    public function show(){
        echo "这里是oneClass.php的show方法<br/>";
    }

}

test/twoClass.php

<?php

class twoClass{

    public function show(){
        echo "这里是twoClass.php的show方法<br/>";
    }

}

下面7种方式都可以实现自动加载,结果都为:

这里是oneClass.php的show方法
这里是twoClass.php的show方法

方法一:index.php 使用__autoload()魔术方法实现自动加载

<?php
//7.2以后使用这个提示一个警告,Deprecated: __autoload() is deprecated, use spl_autoload_register() instead
function __autoload($classname){
    include './test/'.$classname.'.php';
}

//调用类库如果找不到会自动执行__autoload()
$one = new oneClass();
$one->show();
$two = new twoClass();
$two->show();

运行结果

Deprecated: __autoload() is deprecated, use spl_autoload_register() instead in /Users/lidong/Desktop/wwwroot/test/April/autoload1/index.php on line 5
这里是oneClass.php的show方法
这里是twoClass.php的show方法

总结:在PHP7.2以后使用__autoload()会报一个警告,7.2之前这种方式是没提示的.这种方式,是调用一个找不到的类会自动取调用__autoload()方法然后在方法里面执行include引用,实现自动加载。

方法二:index2.php 使用spl_autoload_register()方法实现自动加载,创建自定义register方法调用

<?php

function register($classname){
    include "./test/{$classname}.php";
}

spl_autoload_register("register");

$one = new oneClass();
$one->show();
$two = new twoClass();
$two->show();

方法三:index3.php 使用spl_autoload_register()方法,不定义register方法直接使用回调

<?php

spl_autoload_register(function($classname){
    include "./test/{$classname}.php";
});

$one = new oneClass();
$one->show();
$two = new twoClass();
$two->show();

方法四:index4.php 使用spl_autoload_register()方法,调用类的register方法实现自动加载

class autoLoad{
    public static function register($classname){
        include "./test/{$classname}.php";
    } 
}

spl_autoload_register(["autoLoad","register"]);

$one = new oneClass();
$one->show();
$two = new twoClass();
$two->show();

二、使用命名空间的几种实现

test2/oneClass.php

<?php

namespace auto	est2;
class oneClass{

    public function show(){
        echo "这里是oneClass.php的show方法<br/>";
    }

}

test2/twoClass.php

<?php
namespace auto	est2;
class twoClass{

    public function show(){
        echo "这里是twoClass.php的show方法<br/>";
    }

}

方法五:index5.php,使用spl_autoload_register(),调用加载类的register方法,转化传递过来的命名空间实现自动加载

<?php

class autoLoad{
    public static function register($classname){
        $arr = explode('\', $classname);
        include "./test2/{$arr[2]}.php";
    } 
}

spl_autoload_register(["autoLoad","register"]);

$one = new auto	est2oneClass();
$one->show();
$two = new auto	est2	woClass();
$two->show();

方法六:index6.php 跟方法五类似,区别是use方法调用类实例化时可以直接使用类名,实现自动加载

<?php

use auto	est2oneClass;
use auto	est2	woClass;

class autoLoad{
    public static function register($classname){
        $arr = explode('\', $classname);
        include "./test2/{$arr[2]}.php";
    } 
}

spl_autoload_register(["autoLoad","register"]);

$one = new oneClass();
$one->show();
$two = new twoClass();
$two->show();

方法七:index7.php 与方法五和六思路一致,只不过加载类放在外部不是引用在统一文件,要点就是命名空间定义的类,要使用也要先include,实现自动加载

autoLoad.php
<?php

namespace auto;
class autoLoad{
    public static function register($classname){
        $arr = explode('\', $classname);
        include "./test2/{$arr[2]}.php";
    } 
}
index7.php
<?php
use auto	est2oneClass;
use auto	est2	woClass;

include "./autoLoad.php";

spl_autoload_register(["autoautoLoad","register"]);

$one = new oneClass();
$one->show();
$two = new twoClass();
$two->show();

总结:所有的自动加载思想都是调用一个没引用的类库会,PHP会自动执行的一个加载方法,这个方法有可能是类的方法也有可能是普通方法,但不管怎么样都最终使用include执行文件包含,只不过命名空间需要转化下获取类名。另外值得注意的是,如果是一个php的框架自动加载实现也基本一致,只不过他会根据不同文件夹下面的定义判断后include来实现不同文件夹下文件的引用,来实现整个框架的自动加载。

原文地址:https://www.cnblogs.com/lisqiong/p/10763793.html