為什麼要用spl_autoload_register來取代__autoload()

來源:互聯網
上載者:User
為什麼要用 spl_autoload_register來取代 __autoload()
除了可以多次註冊之外

回複內容:

為什麼要用spl_autoload_register來取代__autoload()
除了可以多次註冊之外

If there must be multiple autoload functions, spl_autoload_register() allows for this. It effectively creates a queue of autoload functions, and runs through each of them in the order they are defined. By contrast, __autoload() may only be defined once.

spl_autoload_register 可以很好地處理需要多個載入器的情況,這種情況下 spl_autoload_register 會按順序依次調用之前註冊過的載入器。作為對比, __autoload 因為是一個函數,所以只能被定義一次。

這是英文文檔裡的一句話,解釋了加入 spl_autoload_register 的意義,但是好像在中文文檔中沒有。

http://www.jb51.net/article/37746.htm
這裡說得很詳細

spl_autoload_register
(PHP 5 >= 5.1.2)
spl_autoload_register — 註冊__autoload()函數
說明
bool spl_autoload_register ([ callback $autoload_function ] )
將函數註冊到SPL __autoload函數棧中。如果該棧中的函數尚未啟用,則啟用它們。
如果在你的程式中已經實現了__autoload函數,它必須顯式註冊到__autoload棧中。因為
spl_autoload_register()函數會將Zend Engine中的__autoload函數取代為spl_autoload()或
spl_autoload_call()。

autoload_function
註冊的自動裝載函數。如果沒有提供任何參數,則自動註冊autoload的預設實現函數
spl_autoload()。
說明
如果成功則返回 TRUE,失敗則返回 FALSE。
註:SPL是Standard PHP Library(標準PHP庫)的縮寫。它是PHP5引入的一個擴充庫,其主要功能包括autoload機制的實現及包括各種Iterator介面或類。SPL autoload機制的實現是通過將函數指標autoload_func指向自己實現的具有自動裝載功能的函數來實現的。SPL有兩個不同的函數spl_autoload, spl_autoload_call,通過將autoload_func指向這兩個不同的函數地址來實現不同的自動載入機制!

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.