Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1997550
  • 博文数量: 433
  • 博客积分: 918
  • 博客等级: 准尉
  • 技术积分: 3218
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-24 18:21
个人简介

你是不是暗恋我,那就给我发个消息呀,让我知道o(∩∩)o

文章分类

全部博文(433)

分类: PHP

2014-12-21 22:18:39

http://haroldphp.iteye.com/blog/1563824
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指向这两个不同的函数地址来实现不同的自动加载机制。 

Php代码  收藏代码
  1. classLOAD  
  2. {  
  3.  staticfunctionloadClass($class_name)  
  4.     {  
  5.         $filename$class_name.".class.php";  
  6.  $path"include/".$filename  
  7.         if(is_file($path)) returninclude$path;  
  8.     }  
  9. }  
  10.    
  11. /** 
  12.  * 设置对象的自动载入 
  13.  * spl_autoload_register — Register given function as __autoload() implementation 
  14.  */  
  15. spl_autoload_register(array('LOAD''loadClass'));  
  16.    
  17. /** 
  18. *__autoload 方法在 spl_autoload_register 后会失效,因为 autoload_func 函数指针已指向 spl_autoload 方法 
  19. * 可以通过下面的方法来把 _autoload 方法加入 autoload_functions list 
  20. */  
  21. spl_autoload_register( '__autoload');  


如果同时用spl_autoload_register注册了一个类的方法和__autoload函数, 
那么,会根据注册的先后,如果在第一个注册的方法或函数里加载了类文件,就不会再执行第二个被注册的类的方法或函数。反之就会执行第二个被注册的类的方法或函数。 

Php代码  收藏代码
  1. class autoloader {    
  2.     public static $loader;    
  3.         
  4.     public static function init() {    
  5.         if (self::$loader == NULL)    
  6.             self::$loader = new self ();    
  7.             
  8.         return self::$loader;    
  9.     }    
  10.         
  11.     public function __construct() {    
  12.         spl_autoload_register ( array ($this'model' ) );    
  13.         spl_autoload_register ( array ($this'helper' ) );    
  14.         spl_autoload_register ( array ($this'controller' ) );    
  15.         spl_autoload_register ( array ($this'library' ) );    
  16.     }    
  17.         
  18.     public function library($class) {    
  19.         set_include_path ( get_include_path () . PATH_SEPARATOR . '/lib/' );    
  20.         spl_autoload_extensions ( '.library.php' );    
  21.         spl_autoload ( $class );    
  22.     }    
  23.         
  24.     public function controller($class) {    
  25.         $class = preg_replace ( '/_controller$/ui'''$class );    
  26.             
  27.         set_include_path ( get_include_path () . PATH_SEPARATOR . '/controller/' );    
  28.         spl_autoload_extensions ( '.controller.php' );    
  29.         spl_autoload ( $class );    
  30.     }    
  31.         
  32.     public function model($class) {    
  33.         $class = preg_replace ( '/_model$/ui'''$class );    
  34.             
  35.         set_include_path ( get_include_path () . PATH_SEPARATOR . '/model/' );    
  36.         spl_autoload_extensions ( '.model.php' );    
  37.         spl_autoload ( $class );    
  38.     }    
  39.         
  40.     public function helper($class) {    
  41.         $class = preg_replace ( '/_helper$/ui'''$class );    
  42.             
  43.         set_include_path ( get_include_path () . PATH_SEPARATOR . '/helper/' );    
  44.         spl_autoload_extensions ( '.helper.php' );    
  45.         spl_autoload ( $class );    
  46.     }    
  47.    
  48. }    
  49.    
  50. //call    
  51. autoloader::init ();    
  52. ?>    
阅读(1923) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~