PHP中spl_autoload_register函数的用法

来源:互联网 发布:virtualbox 安装mac 编辑:程序博客网 时间:2024/05/18 20:12
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代码  收藏代码

    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代码  收藏代码

    <?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 ();    
    ?>
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 租的房子床坏了怎么办 宜家定时器不响怎么办 新插座插不进去怎么办 本溪人社app打不开怎么办 南宁电车超过上牌时间怎么办 苹果6s降频怎么办 0首付手机还不起怎么办 乐才app登录不上怎么办 买了笔记本网要怎么办? 电脑连接不上网络怎么办 电脑上没网络了怎么办 手机返回键不好使怎么办 笔记本无线网连接受限怎么办 魅族打电话图标没有了怎么办 京东价格保护后发票怎么办 淘宝未满十八岁怎么办 SVN提交时代码冲突怎么办 京东自营没货了怎么办 京东下了单 没货怎么办 万达广场购物卡怎么办 京东白条没额度怎么办 京东退款还收到货怎么办 退货不想要货了怎么办 安装微擎创建数据库失败怎么办 tp文件损坏或丢失怎么办 手机QQ启动有问题应该怎么办 微信网页版页面巨大怎么办 魔兽军团烹饪食谱扔了怎么办 赴港旅游网上签证怎么办了? 美团商家入住没有营业执照怎么办 团购房子不想要怎么办 乐视液晶屏坏了怎么办 wifi盒子插上网线然后怎么办 在泰国手机坏了怎么办 朋友在泰国手机关机怎么办 qq上转账转错了怎么办 扣扣转账转错了怎么办 到付快递签收了怎么办 京东白条消费了怎么办 确认收货点错了怎么办 小米6后盖缝隙大怎么办