background image

 
__autoload 的最大缺陷是无法有多个 autoload

 

方法

 

 

好了, 想下下面的这个情景,你的项目引用了别人的一个项目,你的项目中有一个
__autoload,别人的项目也有一个__autoload,这样两个__autoload 就冲突了。解决的办法就
是修改__autoload

 

成为一个,这无疑是非常繁琐的。

 
因此我们急需使用一个 autoload 调用堆栈,这样 spl 的 autoload 系列函数就出现了。你可以
使用 spl_autoload_register 注册多个自定义的 autoload

 

函数

 
如果你的 PHP 版本大于 5.1 的话,你就可以使用 spl_autoload 
 
先了解 spl

 

的几个函数:

 

 
spl_autoload 是_autoload()的默认实现,它会去 include_path 中寻找

$class_name

(.php/.inc) 

Spl_autoload

 

实现自动加载:

代码如下:
 

/*http.php*/

 

<?php 

class

 http 

public

 

function

 callname(){ 

echo

 "this is http"; 


/*test.php*/

 

<?php 
set_include_path("/home/yejianfeng/handcode/"); 

//这里需要将路径放入 include 

spl_autoload("http"); 

//寻找/home/yejianfeng/handcode/http.php 

$a

 = 

new

 http(); 

$a

->callname();