如何使用PHP中的spl_autoload_register()和__autoload()函數(shù)

本篇內(nèi)容介紹了“如何使用PHP中的spl_autoload_register() 和 __autoload() 函數(shù)”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

成都創(chuàng)新互聯(lián)公司主營(yíng)巴州網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,成都App定制開(kāi)發(fā),巴州h5小程序開(kāi)發(fā)搭建,巴州網(wǎng)站營(yíng)銷推廣歡迎巴州等地區(qū)企業(yè)咨詢

如何使用PHP中的spl_autoload_register() 和 __autoload() 函數(shù)

在日常開(kāi)發(fā)使用時(shí),我們面向?qū)ο蟪绦蛟O(shè)計(jì)的基本思想是,通常情況下習(xí)慣為每個(gè)類都創(chuàng)建一個(gè)單獨(dú)的PHP源文件,這樣的話為后來(lái)的維護(hù)提供便利,同時(shí)也很容易對(duì)類進(jìn)行復(fù)用。

在PHP中可以通過(guò)spl_autoload_register() __autoload() 函數(shù)來(lái)實(shí)現(xiàn)類的自動(dòng)加載功能,這樣可以節(jié)省我們的編程時(shí)間和精力。那接下來(lái)我們就分別來(lái)介紹一下這兩個(gè)函數(shù)吧。

__autoload()函數(shù)

__autoload()函數(shù)準(zhǔn)確來(lái)說(shuō)它是魔術(shù)方法,我們?cè)凇段宸昼妿懔私釶HP中的魔術(shù)方法(實(shí)例詳解)》中詳細(xì)的介紹了一些常用的魔術(shù)方法,其中講到,它是自動(dòng)調(diào)用的,也就是需要早特定條件下才會(huì)調(diào)用函數(shù)。

當(dāng)我們 new 一個(gè)類時(shí),如果當(dāng)前源文件中找不到這個(gè)類,PHP 則會(huì)自動(dòng)調(diào)用 __autoload() 函數(shù),并將類名傳遞給 __autoload() 函數(shù)。這就是__autoload()函數(shù)調(diào)用的特定條件。它的語(yǔ)法格式如下:

function __autoload($class){
    // 方法體
}

其中我們需要注意的是:

  • $class 為要加載的類名。

  • __autoload() 函數(shù)在當(dāng)前源文件中只能定義一次。

  • 想要使用 __autoload() 函數(shù)自動(dòng)加載類文件,類文件的名稱需要與類名相同,另外一個(gè)類文件中只能定義一個(gè)類。

接下來(lái)我們通過(guò)示例來(lái)看一下__autoload() 函數(shù)的使用,示例如下:

<?php
    function __autoload($class){
        $file = './'.$class.'.php';
        include_once($file);
    }
    $obj = new Demo();
?>

運(yùn)行上面的代碼,會(huì)自動(dòng)加載同目錄下的 Demo.php 文件,Demo.php 中的代碼如下所示:

<?php
    class Demo{
    }
?>

其中我們需要注意的是:__autoload() 函數(shù)自 PHP7.2.0 起已被棄用,可以使用 spl_autoload_register() 函數(shù)代替。

spl_autoload_register() 函數(shù)

spl_autoload_register()函數(shù)可以指定一個(gè)函數(shù)來(lái)替代__autoload()函數(shù)的功能,

spl_autoload_register([$autoload_function [, $throw = true [, $prepend = false ]]])

其中需要注意的是:

  • $autoload_function:要替代 __autoload() 函數(shù)的函數(shù)名稱,也可以是一個(gè)匿名函數(shù)。如果沒(méi)有提供任何參數(shù),則自動(dòng)注冊(cè) autoload 的默認(rèn)實(shí)現(xiàn)函數(shù) spl_autoload();

  • $throw:用來(lái)設(shè)置 $autoload_function 無(wú)法成功注冊(cè)時(shí),spl_autoload_register() 函數(shù)是否拋出異常;

  • $prepend:如果是 true,則 spl_autoload_register() 函數(shù)會(huì)添加 $autoload_function 函數(shù)到隊(duì)列之首,否則添加到隊(duì)列尾部。

接下來(lái)我們通過(guò)示例來(lái)看一下,示例如下:

<?php
    spl_autoload_register('loadClass');
    function loadClass($class){
        $file = './'.$class.'.php';
        include_once($file);
    }
    $obj = new Demo();
?>

上述示例中使用 spl_autoload_register() 函數(shù)指定另一個(gè)函數(shù)來(lái)替代 __autoload() 函數(shù)。

“如何使用PHP中的spl_autoload_register() 和 __autoload() 函數(shù)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

網(wǎng)頁(yè)題目:如何使用PHP中的spl_autoload_register()和__autoload()函數(shù)
網(wǎng)頁(yè)鏈接:http://muchs.cn/article36/gdepsg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)、商城網(wǎng)站、網(wǎng)站排名營(yíng)銷型網(wǎng)站建設(shè)、做網(wǎng)站、企業(yè)網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)