PHP函数: spl_autoload_register与autoload的功能详解

30 篇文章 1 订阅
12 篇文章 0 订阅

在了解这个函数之前先来看另一个函数:__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();
?>


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 的意义,但是好像在中文文档中没有。

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指向这两个不同的函数地址来实现不同的自动加载机制。
范例
设我们有一个类文件A.php,里面定义了一个名字为A的类:
复制代码 代码如下:

<?php
class A
{
public function __construct()
{
echo 'Got it.';
}
}



然后我们有一个index.php需要用到这个类A,常规的写法就是
复制代码 代码如下:

<?php
require('A.php');
$a = new A();

但是有一个问题就是,假如我们的index.php需要包含的不只是类A,而是需要很多类,这样子就必须写很多行require语句,有时候也会让人觉得不爽。
不过在php5之后的版本,我们就不再需要这样做了。在php5中,试图使用尚未定义的类时会自动调用autoload函数,所以我们可以通过编写__autoload函数来让php自动加载类,而不必写一个长长的包含文件列表。
例如在上面那个例子中,index.php可以这样写:
复制代码 代码如下:

<?php
function __autoload($class)
{
$file = $class . '.php';
if (is_file($file)) {
require_once($file);
}
}
$a = new A();


当然上面只是最简单的示范,__autoload只是去include_path寻找类文件并加载,我们可以根据自己的需要定义__autoload加载类的规则。
此外,假如我们不想自动加载的时候调用__autoload,而是调用我们自己的函数(或者类方法),我们可以使用spl_autoload_register来注册我们自己的autoload函数。它的函数原型如下:
bool spl_autoload_register ( [callback $autoload_function] )
我们继续改写上面那个例子:
复制代码 代码如下:

<?php
function loader($class)
{
$file = $class . '.php';
if (is_file($file)) {
require_once($file);
}
}
spl_autoload_register('loader');
$a = new A();


这样子也是可以正常运行的,这时候php在寻找类的时候就没有调用__autoload而是调用我们自己定义的函数loader了。同样的道理,下面这种写法也是可以的:
复制代码 代码如下:
<?php   
class Loader   
{   
public static function loadClass($class)   
{   
$file = $class . '.php';   
if (is_file($file)) {   
require_once($file);   
}   
}   
}   
spl_autoload_register(array('Loader', 'loadClass'));   
$a = new A();


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值