PHP函數(shù)spl_autoload_register()用法和__autoload()介紹
__autoload()的用法就不再說了,以前已經(jīng)在我的WEB開發(fā)筆記中說過。PHP __autoload函數(shù)(自動載入類文件)的使用方法, 原文地址:http://chabaoo.cn/article/29625.htm 。
說一說,spl_autoload_register()的用法吧,很簡單,可以這樣理解,就是聲明一個(gè)自定義__autoload(),你可以是A函數(shù),也可以是B函數(shù),想怎么樣就怎么樣,函數(shù)體的寫法,當(dāng)然要和__autoload()一樣就OK 了。
當(dāng)PHP找不到類文件會調(diào)用這個(gè)方法,當(dāng)注冊了自己的函數(shù)或方法時(shí),PHP不會調(diào)用__autoload()函數(shù),而會調(diào)用自定義的函數(shù)
spl_autoload_register(‘func_name');
spl_autoload_register(array(‘class_name','method_name'));
詳細(xì)說明如下:
spl_autoload_register
(PHP 5>= 5.1.2)
spl_autoload_register — 注冊__autoload()函數(shù)
說明
bool spl_autoload_register ([ callback $autoload_function ] )
將函數(shù)注冊到SPL __autoload函數(shù)棧中。如果該棧中的函數(shù)尚未激活,則激活它們。
如果在你的程序中已經(jīng)實(shí)現(xiàn)了__autoload函數(shù),它必須顯式注冊到__autoload棧中。因?yàn)?
spl_autoload_register()函數(shù)會將Zend Engine中的__autoload函數(shù)取代為spl_autoload()或
spl_autoload_call()。
參數(shù)
autoload_function
欲注冊的自動裝載函數(shù)。如果沒有提供任何參數(shù),則自動注冊autoload的默認(rèn)實(shí)現(xiàn)函數(shù)
spl_autoload()。
返回值
如果成功則返回 TRUE,失敗則返回 FALSE。
注:SPL是Standard PHP Library(標(biāo)準(zhǔn)PHP庫)的縮寫。它是PHP5引入的一個(gè)擴(kuò)展庫,其主要功能包括autoload機(jī)制的實(shí)現(xiàn)及包括各種Iterator接口或類。SPL autoload機(jī)制的實(shí)現(xiàn)是通過將函數(shù)指針autoload_func指向自己實(shí)現(xiàn)的具有自動裝載功能的函數(shù)來實(shí)現(xiàn)的。SPL有兩個(gè)不同的函數(shù)spl_autoload, spl_autoload_call,通過將autoload_func指向這兩個(gè)不同的函數(shù)地址來實(shí)現(xiàn)不同的自動加載機(jī)制。
代碼如下:
test.class.php
<?php class abc{ function __construct() { echo 'www.chhua.com; } } ?>
load.php
<?php class LOAD { static function loadClass($class_name) { $filename = $class_name.".class.php"; if (is_file($filename)) return include_once $filename; } } /** * 設(shè)置對象的自動載入 * spl_autoload_register — Register given function as __autoload() implementation */ spl_autoload_register(array('LOAD', 'loadClass')); $a = new Test();//實(shí)現(xiàn)自動加載,很多框架就用這種方法自動加載類 ?>
spl_autoload_register裝載函數(shù)的正確寫法
AutoLoading\loading
<?php namespace AutoLoading; class Loadind { public static function autoload($className){ //根據(jù)PSR-O的第4點(diǎn) 把 \ 轉(zhuǎn)換層(目錄風(fēng)格符) DIRECTORY_SEPARATOR , //便于兼容Linux文件找。Windows 下(/ 和 \)是通用的 //由于namspace 很規(guī)格,所以直接很快就能找到 $fileName = str_replace('\\', DIRECTORY_SEPARATOR, DIR . '\\'. $className) . '.php'; if (is_file($fileName)) { require $fileName; } else { echo $fileName . ' is not exist'; die; } } }
index.php
//定義當(dāng)前的目錄絕對路徑 define('DIR', dirname(__FILE__)); //加載這個(gè)文件 require DIR . '/loading.php'; //采用`命名空間`的方式注冊。php 5.3 加入的 //也必須是得是static靜態(tài)方法調(diào)用,然后就像加載namespace的方式調(diào)用,注意:不能使用use spl_autoload_register("\\AutoLoading\\loading::autoload"); // 調(diào)用三個(gè)namespace類 //定位到Lib目錄下的Name.php Lib\Name::test(); //定位到App目錄下Android目錄下的Name.php App\Android\Name::test(); //定位到App目錄下Ios目錄下的Name.php App\Ios\Name::test();
- PHP SPL使用方法和他的威力
- php中spl_autoload詳解
- PHP spl_autoload_register實(shí)現(xiàn)自動加載研究
- PHP SPL標(biāo)準(zhǔn)庫之文件操作(SplFileInfo和SplFileObject)實(shí)例
- PHP中spl_autoload_register()函數(shù)用法實(shí)例詳解
- 解析PHP SPL標(biāo)準(zhǔn)庫的用法(遍歷目錄,查找固定條件的文件)
- PHP SPL標(biāo)準(zhǔn)庫之?dāng)?shù)據(jù)結(jié)構(gòu)棧(SplStack)介紹
- PHP SPL標(biāo)準(zhǔn)庫之SplFixedArray使用實(shí)例
- PHP SPL標(biāo)準(zhǔn)庫中的常用函數(shù)介紹
- PHP SPL標(biāo)準(zhǔn)庫之接口(Interface)詳解
- PHP SPL 被遺落的寶石【SPL應(yīng)用淺析】
相關(guān)文章
PHP利用ChatGPT實(shí)現(xiàn)輕松創(chuàng)建用戶注冊頁面
ChatGPT?是?OpenAI?開發(fā)的?GPT語言模型的變體,它是一種大型單向語言模型。本文就來利用ChatGPT創(chuàng)建簡單的用戶注冊頁面,感興趣的可以了解一下2023-02-02php實(shí)現(xiàn)猴子選大王問題算法實(shí)例
這篇文章主要介紹了php實(shí)現(xiàn)猴子選大王問題算法,實(shí)例分析了算法的原理與解決方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04iis6手工創(chuàng)建網(wǎng)站后無法運(yùn)行php腳本的解決方法
下面小編就為大家?guī)硪黄猧is6手工創(chuàng)建網(wǎng)站后無法運(yùn)行php腳本的解決方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06PHP 函數(shù)學(xué)習(xí)簡單小結(jié)
下面是一些php下經(jīng)常用的函數(shù),都是些必須要知道的函數(shù),只有知道有個(gè)函數(shù)與功能,才可能組裝成完整的功能強(qiáng)大的系統(tǒng)。2010-07-07在Mac OS下搭建LNMP開發(fā)環(huán)境的步驟詳解
這篇文章主要介紹了在Mac OS下搭建LNMP開發(fā)環(huán)境的步驟,文中通過一步步的步驟介紹的非常詳細(xì),對大家具有一定的參考價(jià)值,需要的朋友們下面來一起看看吧。2017-03-03PHP設(shè)計(jì)模式之模板方法模式Template Method Pattern詳解
在我們實(shí)際開發(fā)中,如果一個(gè)方法極其復(fù)雜時(shí),如果我們將所有的邏輯寫在一個(gè)方法中,那維護(hù)起來就很困難,要替換某些步驟時(shí)都要重新寫,這樣代碼的擴(kuò)展性就很差,當(dāng)遇到這種情況就要考慮今天的主角——模板方法模式2022-12-12