這篇文章主要介紹了PHP中spl_autoload_register()函數用法,結合實例形式分析了__autoload函數及spl_autoload_register函數的相關使用技巧,需要的朋友可以參考下
推薦手冊:php完全自學手冊
在了解這個函數之前先來看另一個函數:__autoload。
一、__autoload
這是一個自動載入函數,在PHP5中,當我們實例化一個未定義的類別時,就會觸發此函數。請看下面範例:
printit.class.php:
<?php class PRINTIT { function doPrint() { echo 'hello world'; } } ?>
index.php
<? function __autoload( $class ) { $file = $class . '.class.php'; if ( is_file($file) ) { require_once($file); } } $obj = new PRINTIT(); $obj->doPrint();?>
運行index.php後正常輸出hello world。在index.php中,由於沒有包含printit.class.php,在實例化printit時,自動呼叫__autoload函數,參數$class的值即為類別名稱printit,此時printit.class.php就被引進來了。
在物件導向中這種方法經常使用,可以避免書寫過多的引用文件,同時也使整個系統更加靈活。
二、spl_autoload_register()
再看spl_autoload_register(),這個函數與__autoload有與曲同工之妙,看個簡單的例子:
<? function loadprint( $class ) { $file = $class . '.class.php'; if (is_file($file)) { require_once($file); } } spl_autoload_register( 'loadprint' ); $obj = new PRINTIT(); $obj->doPrint();?>
將__autoload換成loadprint函數。但loadprint不會像__autoload自動觸發,這時spl_autoload_register()就運作了,它告訴PHP碰到沒有定義的類別就執行loadprint()。
spl_autoload_register() 呼叫靜態方法
<? class test { public static function loadprint( $class ) { $file = $class . '.class.php'; if (is_file($file)) { require_once($file); } } } spl_autoload_register( array('test','loadprint') ); //另一种写法:spl_autoload_register( "test::loadprint" ); $obj = new PRINTIT(); $obj->doPrint();?>
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指向這兩個不同的函數位址來實現不同的自動載入機制。
classLOAD { staticfunctionloadClass($class_name) { $filename= $class_name.".class.php"; $path= "include/".$filename if(is_file($path)) returninclude$path; } } /** * 设置对象的自动载入 * spl_autoload_register — Register given function as __autoload() implementation */ spl_autoload_register(array('LOAD', 'loadClass')); /** *__autoload 方法在 spl_autoload_register 后会失效,因为 autoload_func 函数指针已指向 spl_autoload 方法 * 可以通过下面的方法来把 _autoload 方法加入 autoload_functions list */ spl_autoload_register( '__autoload');
如果同時用spl_autoload_register註冊了一個類別的方法和__autoload函數,那麼,會根據註冊的先後,如果在第一個註冊的方法或函數裡加載了類別文件,就不會再執行第二個被註冊的類別的方法或函數。反之就會執行第二個被註冊的類別的方法或函數。
<?php class autoloader { public static $loader; public static function init() { if (self::$loader == NULL) self::$loader = new self (); return self::$loader; } public function __construct() { spl_autoload_register ( array ($this, 'model' ) ); spl_autoload_register ( array ($this, 'helper' ) ); spl_autoload_register ( array ($this, 'controller' ) ); spl_autoload_register ( array ($this, 'library' ) ); } public function library($class) { set_include_path ( get_include_path () . PATH_SEPARATOR . '/lib/' ); spl_autoload_extensions ( '.library.php' ); spl_autoload ( $class ); } public function controller($class) { $class = preg_replace ( '/_controller$/ui', '', $class ); set_include_path ( get_include_path () . PATH_SEPARATOR . '/controller/' ); spl_autoload_extensions ( '.controller.php' ); spl_autoload ( $class ); } public function model($class) { $class = preg_replace ( '/_model$/ui', '', $class ); set_include_path ( get_include_path () . PATH_SEPARATOR . '/model/' ); spl_autoload_extensions ( '.model.php' ); spl_autoload ( $class ); } public function helper($class) { $class = preg_replace ( '/_helper$/ui', '', $class ); set_include_path ( get_include_path () . PATH_SEPARATOR . '/helper/' ); spl_autoload_extensions ( '.helper.php' ); spl_autoload ( $class ); } } //call autoloader::init (); ?>
總結:以上就是這篇文章的全部內容,希望能對大家的學習有所幫助。
相關文章推薦:
1.PHP自動載入機制介紹-spl_autoload_register()函數,php類別自動載入
2.如何使用spl_autoload_register實作自動載入實例詳解
相關影片推薦:
1.獨孤九賤(4)_PHP影片教學
以上是關於PHP中spl_autoload_register()函數用法詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

Dreamweaver CS6
視覺化網頁開發工具

禪工作室 13.0.1
強大的PHP整合開發環境

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

Atom編輯器mac版下載
最受歡迎的的開源編輯器