詳解spl_autoload_register()函式
在瞭解這個函式之前先來看另一個函式:__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();
?>
轉:http://pingzhao1990.blog.163.com/blog/static/11356634220126219184282/
在瞭解這個函式之前先來看另一個函式:__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(); |
?> |
相關文章
- 尤拉函式詳解函式
- malloc函式詳解函式
- kill() 函式詳解函式
- ioctl()函式詳解函式
- gluLookAt 函式詳解函式
- fopencookie函式詳解Cookie函式
- 詳解Java函式式介面Java函式
- Socket send函式和recv函式詳解函式
- 建構函式詳解函式
- 函式引數詳解函式
- mysql常用函式詳解MySql函式
- 箭頭函式詳解函式
- fcntl函式用法詳解函式
- JavaScript函式模式詳解JavaScript函式模式
- eval()函式用法詳解函式
- Oracle 聚合函式詳解Oracle函式
- wait()函式詳解AI函式
- 詳解JavaScript函式模式JavaScript函式模式
- oracle TRANSLATE函式詳解Oracle函式
- select 函式詳解函式
- xslt函式詳解函式
- Oracle 分析函式詳解Oracle函式
- PHP函式處理函式例項詳解PHP函式
- Oracle分析函式之開窗函式over()詳解Oracle函式
- ORALCE函式:LAG()和LEAD() 分析函式詳解函式
- python socket函式詳解Python函式
- TypeScript中的函式詳解TypeScript函式
- 箭頭函式this指向詳解函式
- Java建構函式詳解Java函式
- vimscript-expand函式詳解函式
- Oracle單行函式詳解Oracle函式
- setdefaultencoding函式使用詳解Encoding函式
- 詳解C語言函式C語言函式
- PHP BC Math 函式詳解PHP函式
- getpid()函式用法詳解函式
- ORACLE to_char函式詳解Oracle函式
- oracle over函式 詳解(轉)Oracle函式
- Oracle中pivot函式詳解Oracle函式