PHP中spl_autoload_register()函数用法实例详解
本文实例分析了PHP中spl_autoload_register()函数用法。分享给大家供大家参考,具体如下:
在了解这个函数之前先来看另一个函数:__autoload。
一、__autoload
这是一个自动加载函数,在PHP5中,当我们实例化一个未定义的类时,就会触发此函数。看下面例子:
printit.class.php:
<?php classPRINTIT{ functiondoPrint(){ echo'helloworld'; } } ?>
index.php
<? function__autoload($class){ $file=$class.'.class.php'; if(is_file($file)){ require_once($file); } } $obj=newPRINTIT(); $obj->doPrint();?>
运行index.php后正常输出helloworld。在index.php中,由于没有包含printit.class.php,在实例化printit时,自动调用__autoload函数,参数$class的值即为类名printit,此时printit.class.php就被引进来了。
在面向对象中这种方法经常使用,可以避免书写过多的引用文件,同时也使整个系统更加灵活。
二、spl_autoload_register()
再看spl_autoload_register(),这个函数与__autoload有与曲同工之妙,看个简单的例子:
<? functionloadprint($class){ $file=$class.'.class.php'; if(is_file($file)){ require_once($file); } } spl_autoload_register('loadprint'); $obj=newPRINTIT(); $obj->doPrint();?>
将__autoload换成loadprint函数。但是loadprint不会像__autoload自动触发,这时spl_autoload_register()就起作用了,它告诉PHP碰到没有定义的类就执行loadprint()。
spl_autoload_register()调用静态方法
<? classtest{ publicstaticfunctionloadprint($class){ $file=$class.'.class.php'; if(is_file($file)){ require_once($file); } } } spl_autoload_register(array('test','loadprint')); //另一种写法:spl_autoload_register("test::loadprint"); $obj=newPRINTIT(); $obj->doPrint();?>
spl_autoload_register
(PHP5>=5.1.2)
spl_autoload_register—注册__autoload()函数
说明
boolspl_autoload_register([callback$autoload_function])
将函数注册到SPL__autoload函数栈中。如果该栈中的函数尚未激活,则激活它们。
如果在你的程序中已经实现了__autoload函数,它必须显式注册到__autoload栈中。因为spl_autoload_register()函数会将ZendEngine中的__autoload函数取代为spl_autoload()或spl_autoload_call()。
参数
autoload_function
欲注册的自动装载函数。如果没有提供任何参数,则自动注册autoload的默认实现函数spl_autoload()。
返回值
如果成功则返回TRUE,失败则返回FALSE。
注:SPL是StandardPHPLibrary(标准PHP库)的缩写。它是PHP5引入的一个扩展库,其主要功能包括autoload机制的实现及包括各种Iterator接口或类。SPLautoload机制的实现是通过将函数指针autoload_func指向自己实现的具有自动装载功能的函数来实现的。SPL有两个不同的函数spl_autoload,spl_autoload_call,通过将autoload_func指向这两个不同的函数地址来实现不同的自动加载机制。
classLOAD { staticfunctionloadClass($class_name) { $filename=$class_name.".class.php"; $path="include/".$filename if(is_file($path))returninclude$path; } } /** *设置对象的自动载入 *spl_autoload_register—Registergivenfunctionas__autoload()implementation */ spl_autoload_register(array('LOAD','loadClass')); /** *__autoload方法在spl_autoload_register后会失效,因为autoload_func函数指针已指向spl_autoload方法 *可以通过下面的方法来把_autoload方法加入autoload_functionslist */ spl_autoload_register('__autoload');
如果同时用spl_autoload_register注册了一个类的方法和__autoload函数,那么,会根据注册的先后,如果在第一个注册的方法或函数里加载了类文件,就不会再执行第二个被注册的类的方法或函数。反之就会执行第二个被注册的类的方法或函数。
<?php classautoloader{ publicstatic$loader; publicstaticfunctioninit(){ if(self::$loader==NULL) self::$loader=newself(); returnself::$loader; } publicfunction__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')); } publicfunctionlibrary($class){ set_include_path(get_include_path().PATH_SEPARATOR.'/lib/'); spl_autoload_extensions('.library.php'); spl_autoload($class); } publicfunctioncontroller($class){ $class=preg_replace('/_controller$/ui','',$class); set_include_path(get_include_path().PATH_SEPARATOR.'/controller/'); spl_autoload_extensions('.controller.php'); spl_autoload($class); } publicfunctionmodel($class){ $class=preg_replace('/_model$/ui','',$class); set_include_path(get_include_path().PATH_SEPARATOR.'/model/'); spl_autoload_extensions('.model.php'); spl_autoload($class); } publicfunctionhelper($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(); ?>
更多关于PHP相关内容感兴趣的读者可查看本站专题:《php面向对象程序设计入门教程》、《PHP数学运算技巧总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《PHP数据结构与算法教程》、《php程序设计算法总结》、《php正则表达式用法总结》、及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。