PHP中類的自動加載

首先認識一個函數:
spl_autoload_register
官方文檔:https://www.php.net/manual/zh/function.spl-autoload-register.php
spl_autoload_register() 滿足了多條 autoload 的需求。 它實際上創建了 autoload 函數的隊列,按定義時的順序逐個執行。相比之下, __autoload() 只可以定義一次。

魔術方法__autoload

沒啥好說的,上段代碼:

function __autoload($class)
{
    // 根據類名確定文件名
    $file = $class . '.php';
 
    if (file_exists($file)) {
        include $file; // 引入PHP文件
    }
}

spl_autoload_register

我們來模擬一次自動加載:
先建立一個項目,文件夾結構如下:
在這裏插入圖片描述
先寫一個自動加載封裝類:Loader.php

<?php

class  Loader
{
    // 解析命名空間的路徑
    public static $vendorMap = [
        'app' => __DIR__ . DIRECTORY_SEPARATOR . 'app',
    ];

    public function __construct()
    {
        echo __DIR__;die;
    }

    // 自動加載器
    public static function autoload($class)
    {
        $file = self::findFile($class);
        if (file_exists($file)) {
            self::includeFile($file);
        }
    }

    // 解析文件路徑
    private static function findFile($class)
    {
        $vendor = substr($class, 0, strpos($class, '\\')); // 頂級命名空間
        $vendorDir = self::$vendorMap[$vendor]; // 文件基目錄
        $filePath = substr($class, strlen($vendor)) . '.php'; // 文件相對路徑
        return strtr($vendorDir . $filePath, '\\', DIRECTORY_SEPARATOR); // 文件標準路徑
    }

    // 引入文件
    private static function includeFile($file)
    {
        if (is_file($file)) {
            include $file;
        }
    }
}

寫一個簡單的類文件index.php

<?php

namespace app\index\controller\hello;

class Index
{
    function test()
    {
        echo '<h1>hello world</h1>';
    }
}

我們加載一下封裝好的自動加載類,模擬一下index類的自動加載:run.php

<?php
include 'Loader.php'; // 引入加載器
spl_autoload_register('Loader::autoload'); // 註冊自動加載

$indexController =  new app\index\controller\hello\Index(); // 實例化未引用的類
$indexController->test();

我們運行run.php,可以看到index.php可以直接實例化了,而我們的文件中並沒有直接引入他,而只是註冊了一個自動加載器,是不是相當方便吶!

就這,奧利給!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章