【转载修改】PHP的__autoLoad与spl_autoload_*
来源:互联网 发布:vivo一键锁屏软件 编辑:程序博客网 时间:2024/06/05 10:04
__autoLoad
这是一个自动加载函数,当我们实例化一个未定义的类时,就会触发此函数。
__autoload的使用方法1:
最经常使用的就是这种方法,根据类名,找出类文件,然后require
function __autoload($class_name) { $path = str_replace('_', '/', $class_name); require $path . '.php';}// 这里会自动加载Http/File/Interface.php 文件$a = new Http_File_Interface();
这种方法的好处就是简单易使用。当然也有缺点,缺点就是将类名和文件路径强制做了约定,当修改文件结构的时候,就势必要修改类名。
__autoload的使用方法2(直接映射法)
$map = array( 'Http_File_Interface' => 'C:/PHP/HTTP/FILE/Interface.php');function __autoload($class_name) { if (isset($map[$class_name])) { require $map[$class_name]; }}// 这里会自动加载C:/PHP/HTTP/FILE/Interface.php 文件$a = new Http_File_Interface();
这种方法的好处就是类名和文件路径只是用一个映射来维护,所以当文件结构改变的时候,不需要修改类名,只需要将映射中对应的项修改就好了。
这种方法相较于前面的方法缺点是当文件多了的时候这个映射维护起来非常麻烦,或许这时候你就会考虑使用json或者单独一个文件来进行维护了。或许你会想到使用一个框架来维护或者建立这么一个映射。
因此,当有大量的类文件要包含的时候,我们只要确定相应的规则,然后在__autoload()函数中,将类名与实际的磁盘文件对应起来,就可以实现lazy loading的效果。从这里我们也可以看出__autoload()函数的实现中最重要的是类名与实际的磁盘文件映射规则的实现。
SPL与__autoload
__autoload的最大缺陷是无法有多个autoload方法。比如,你的项目引用了别人的一个项目,你的项目中有一个__autoload,别人的项目也有一个__autoload,这样两个__autoload就冲突了。解决的办法就是修改__autoload成为一个,这无疑是非常繁琐的。
因此我们急需使用一个autoload调用堆栈,这样spl的autoload系列函数就出现了。你可以使用spl_autoload_register注册多个自定义的autoload函数。
PHP提供了两种方法来实现自动装载机制,一种我们前面已经提到过,是使用用户定义的__autoload()函数,这通常在PHP源程序中来实现;另外一种就是设计一个函数,将autoload_func指针指向它,这通常使用C语言在PHP扩展中实现。如果既实现了__autoload()函数,又实现了autoload_func(将autoload_func指向某一PHP函数),那么只执行autoload_func函数。
SPL是Standard PHP Library(标准PHP库)的缩写。它是PHP5引入的一个扩展库,其主要功能包括autoload机制的实现及包括各种Iterator接口或类。
SPL autoload机制的实现是通过将函数指针autoload_func指向自己实现的具有自动装载功能的函数来实现的。SPL有两个不同的函数spl_autoload, spl_autoload_call,通过将autoload_func指向这两个不同的函数地址来实现不同的自动加载机制。
首先看下PHP实例化对象的过程,PHP在实例化一个对象时(实际上在实现接口,使用类常数或类中的静态变量,调用类中的静态方法时都会如此),首先会在系统中查找该类(或接口)是否存在,如果不存在的话就尝试使用autoload机制来加载该类。而autoload机制的主要执行过程为:
(1) 检查执行器全局变量函数指针autoload_func是否为NULL。 (2) 如果autoload_func==NULL,
则查找系统中是否定义有__autoload()函数,如果没有,则报告错误并退出。 (3)
如果定义了__autoload()函数,则执行__autoload()尝试加载类,并返回加载结果。 (4)
如果autoload_func不为NULL,则直接执行autoload_func指针指向的函数用来加载类。注意此时并不检查__autoload()函数是否定义。
function load($className){ echo __FUNCTION__.'<br />'; require './http/'.$className.'.php'; } spl_autoload_register('load'); function __autoload($className) { echo __FUNCTION__.'<br />'; require './http/'.$className.'.php'; } $a = new http();//输出load
spl_autoload_*
先了解spl的几个函数:
spl_autoload实现自动加载
void spl_autoload ( string $class_name [, string $file_extensions ] )
在默认情况下,本函数先将类名转换成小写,再在小写的类名后加上 .inc 或 .php 的扩展名作为文件名,然后在所有的包含路径(include paths)中检查是否存在该文件。
【php中函数名和类名不区分大小写,变量名区分】
spl_autoload — __autoload()函数的默认实现【from php.net,不太理解??】
spl_autoload是SPL实现的默认的自动加载函数,它的功能比较简单。它可以接收两个参数,第一个参数是$class_name,表示类名,第二个参数$file_extensions是可选的,表示类文件的扩展名,可以在$file_extensions中指定多个扩展名,护展名之间用逗号隔开,如果不指定的话,它将使用默认的扩展名.inc或.php。
spl_autoload首先将$class_name变为小写,然后在所有的include path中搜索$class_name.inc或$class_name.php文件(如果不指定$file_extensions参数的话),如果找到,就加载该类文件。
你可以手动使用spl_autoload(”Person”, “.class.php”)来加载Person类。实际上,它跟require/include差不多,不同的它可以指定多个扩展名。
// ./http/http.phpclass http{ public function callname(){ echo "this is ./http/http"; } }// ./http/http.class.phpclass http{ public function callname(){ echo "this is ./http/http.class.php"; } }//当前文件夹下./test.phpset_include_path( get_include_path().PATH_SEPARATOR . "/home/yejianfeng/handcode/"); //改成自己的路径,将php文件放入include path//echo get_include_path();//默认第一个是.(当前目录)spl_autoload("http",'.class.php'); //依次寻找include_path下的http.class.php,如果当前目录有http.class.php文件,会优先加载当前目录的//spl_autoload("http");//依次寻找include_path下的http.inc、http.php,(注意默认优先找.inc)//spl_autoload("http",'.php,.inc');//优先找http.php//spl_autoload("http",'.cfg'); //cfg文件中代码同php代码一样,http.cfg//spl_autoload("http",'cfg'); //cfg文件中代码同php代码一样,httpcfg,起连接查找作用$a = new http();$a->callname();
spl_autoload_register
怎样让spl_autoload自动起作用呢,也就是将autoload_func指向spl_autoload?答案是使用spl_autoload_register函数。在PHP脚本中第一次调用spl_autoload_register()时不使用任何参数,就可以将autoload_func指向spl_autoload。
set_include_path( get_include_path().PATH_SEPARATOR . "/data/www/myTest/otherTest/http/"); //将php文件放入include path即可//echo get_include_path();// .:/usr/share/php:/usr/share/pear:/data/www/myTest/otherTest/http/spl_autoload("http",'.class.php'); //依次寻找include_path下的http.class.phpfunction __autoload($className) { echo __FUNCTION__.'<br />'; require ('/data/www/myTest/otherTest/http/'.$className.'.php');}$a = new HTTP();$a->callname();//spl_autoload会自动替代__autoload
通过上面的说明我们知道,spl_autoload的功能比较简单,而且它是在SPL扩展中实现的,我们无法扩充它的功能。如果想实现自己的更灵活的自动加载机制怎么办呢?这时,spl_autoload_call函数闪亮登场了。
spl_autoload_call
void spl_autoload_call ( string $class_name )
可以直接在程序中手动调用此函数来使用所有已注册的__autoload函数装载类或接口。
function load (){ echo __FUNCTION__.'<br />';}spl_autoload_register('load');spl_autoload_register(function($className) { var_dump($className);});spl_autoload_call('ManuallyCalledClass');/*输出,依次调用load和匿名函数,如果匿名函数先register,先调匿名函数*load*string 'ManuallyCalledClass' (length=19)*/
在SPL模块内部,有一个全局变量autoload_functions,它本质上是一个HashTable,不过我们可以将其简单的看作一个链表,链表中的每一个元素都是一个函数指针,指向一个具有自动加载类功能的函数。spl_autoload_call本身的实现很简单,只是简单的按顺序执行这个链表中每个函数,在每个函数执行完成后都判断一次需要的类是否已经加载,如果加载成功就直接返回,不再继续执行链表中的其它函数。如果这个链表中所有的函数都执行完成后类还没有加载,spl_autoload_call就直接退出,并不向用户报告错误。因此,使用了autoload机制,并不能保证类就一定能正确的自动加载,关键还是要看你的自动加载函数如何实现。
spl_autoload_register
那么自动加载函数链表autoload_functions是谁来维护呢?就是前面提到的spl_autoload_register函数。它可以将用户定义的自动加载函数注册到这个链表中,并将autoload_func函数指针指向spl_autoload_call函数。
bool spl_autoload_register ([ callable
autoloadfunction[,bool throw = true [, bool $prepend = false ]]] )autoload_function 欲注册的自动装载函数。如果没有提供任何参数,则自动注册 autoload 的默认实现函数spl_autoload()。
throw 此参数设置了 autoload_function 无法成功注册时,是否抛出异常。
prepend 如果是 true,spl_autoload_register() 会添加函数到队列之首,而不是队列尾部。
__autoload与spl_autoload
set_include_path( get_include_path().PATH_SEPARATOR . "/data/www/myTest/otherTest/http/"); //将php文件放入include path即可spl_autoload_register('__autoload');spl_autoload_register();spl_autoload("http",'.cfg');//虽然先注册的__autoload,但先执行这个,查找http.cfg,没有的话??function __autoload($className) { echo __FUNCTION__.'<br />'; require ('/data/www/myTest/otherTest/http/'.$className.'.php');}$a = new HTTP();$a->callname();
另一种情况:
set_include_path( get_include_path().PATH_SEPARATOR . "/data/www/myTest/otherTest/http/"); //将php文件放入include path即可spl_autoload_register('__autoload');spl_autoload_register();//spl_autoload("http",'.cfg');function __autoload($className) { echo __FUNCTION__.'<br />'; require ('/data/www/myTest/otherTest/http/'.$className.'.php');}$a = new HTTP();$a->callname();//并不报错,调用__autoload
其他特性
//注册一个不存在的函数spl_autoload_register('load',false);//不会报错spl_autoload_register('load');//默认会报错,抛异常echo '<br />++6++<br />';
// ./http/http.phpecho '<br />pre class in ./http/http.php<br />';class http{ public function callname(){ echo "this is ./http/http.php"; } }// ./test.phpset_include_path( get_include_path().PATH_SEPARATOR . "/data/www/myTest/otherTest/http"); //改成自己的路径,将php文件放入include pathvar_dump(spl_autoload_functions());//boolean falsespl_autoload("http"); //输出pre class in ./http/http.php,成功加载//spl_autoload_call('http'); //输出pre class in ./http/http.php,成功加载var_dump(spl_autoload_functions());//boolean false
spl_autoload不受spl_autoload_functions为空数组的影响
// ./http/http.phpecho '<br />./http/http.php<br />';class http{ public function callname(){ echo "this is ./http/http.php"; } }// ./test.phpset_include_path( get_include_path().PATH_SEPARATOR . "/data/www/myTest/otherTest/http"); //改成自己的路径,将php文件放入include pathfunction load($className){ echo __FUNCTION__; //...}spl_autoload_register('load'); //①spl_autoload_unregister('load'); //②var_dump(spl_autoload_functions());spl_autoload_call('http'); //没有输出,没有被加载,因为spl_autoload_functions数组为空spl_autoload("http"); //不受spl_autoload_functions数组的影响
spl_autoload()和spl_autoload_call()是在程序运行到所在行即加载,调用不到Fatal error 、LogicException;而__autoload和register的autoload函数是在new的时候寻找加载相应文件(应该是new的时候调用spl_autoload()和spl_autoload_call())。
spl_autoload_unregister
我们也可以通过spl_autoload_unregister函数将已经注册的函数从autoload_functions链表中删除。
bool spl_autoload_unregister ( mixed $autoload_function )
从spl提供的自动装载函数队列中注销某一函数。如果该函数队列处于激活状态,并且在给定函数注销后该队列变为空,则该函数队列将会变为无效。
如果该函数注销后使得自动装载函数队列无效,即使存在有__autoload函数它也不会自动激活。
function load($className){ echo __FUNCTION__; //...}function __autoload($className){ echo __FUNCTION__; //...}//spl_autoload_register('load'); //①//spl_autoload_unregister('load'); //②$a=new http();//正常会调用__autoload函数,但是当把注释的两行(①②)取消注释后,并不会调用__autoload
spl_autoload_extensions
string spl_autoload_extensions ([ string $file_extensions ] )
— 注册并返回spl_autoload函数使用的默认文件扩展名。
var_dump(spl_autoload_extensions());//string '.inc,.php' (length=9)spl_autoload_extensions('.php,/index.php,.php,.lib,.lib.php');var_dump(spl_autoload_extensions());//string '.php,/index.php,.php,.lib,.lib.php' (length=34)//不必一定要有.(点)
spl_autoload_functions
array spl_autoload_functions ( void )
获取所有已注册的 __autoload() 函数。
function load($className){ echo __FUNCTION__.'<br>'; //...}function load2($className){ echo __FUNCTION__.'<br>'; //...}spl_autoload_register('load');var_dump(spl_autoload_functions());//array (size=1) 0 => string 'load' (length=4)spl_autoload_register('load2',true,true);var_dump(spl_autoload_functions());/*load2跑到首部array (size=2) 0 => string 'load2' (length=5) 1 => string 'load' (length=4)*/spl_autoload_unregister('load');var_dump(spl_autoload_functions());/* array (size=1) 0 => string 'load2' (length=5)*/spl_autoload_register('load3',false);var_dump(spl_autoload_functions());/* array (size=1) 0 => string 'load2' (length=5)*/class testStatic { public static function loadStatic( $class ) { echo __METHOD__.'<br>'; }}class test { public function loadCommon( $class ) { echo __METHOD__.'<br>'; }}spl_autoload_register(array('testStatic','loadStatic'));spl_autoload_register("testStatic::loadStatic"); //另一种写法//spl_autoload_register(array('test','loadprint'));//报错,一般方法需用对象$instance = new test();spl_autoload_register(array($instance, 'loadCommon'));var_dump(spl_autoload_functions());/* array (size=3) 0 => string 'load2' (length=5) 1 => array (size=2) 0 => string 'testStatic' (length=10) 1 => string 'loadStatic' (length=10) 2 => array (size=2) 0 => object(test)[2] 1 => string 'loadCommon' (length=10)*/$a=new http();/* 依次输出: load2 testStatic::loadStatic test::loadCommon 报错*/
小结
创建没有定义的对象: 判断函数指针autoload_func NULL–>__autoload()
不为NULL,忽略__autoload(),执行autoload_func指针指向的函数(autoload_func指针指向spl_autoload_call?)
spl_autoload 查找include_path中文件(默认.inc,.php)
spl_autoload_extensions 注册并返回spl_autoload函数使用的默认文件扩展名spl_autoload_register,增加autoload_functions
spl_autoload_unregister,删除autoload_functions
spl_autoload_call,手动调用autoload_functions中函数
spl_autoload_functions,获得autoload_functions中函数列表
参考
php.net
php自动加载机制autoload
http://blog.csdn.net/teedry/article/details/8064826?locationNum=5
说说PHP的autoLoad
http://www.cnblogs.com/yjf512/archive/2012/09/27/2705260.html
详解spl_autoload_register()函数
http://blog.csdn.net/panpan639944806/article/details/23192267
- 【转载修改】PHP的__autoLoad与spl_autoload_*
- PHP __autoload与spl_autoload
- php __autoload()函数与require 和include() 函数的介绍
- php中__autoload的使用
- PHP关于__autoload的用法
- php __autoload
- php: __autoload 与 sql_autoload_register 自动加载区别
- PHP类的自动加载__autoload && spl_autoload_register
- PHP自动加载__autoload的工作机制
- php中__autoload和spl_autoload_register的用法
- php自动加载__autoload的工作机制
- PHP进阶学习之spl_autoload_register与__autoload方法使用
- PHP函数__autoload失效原因(与smarty有关)
- PHP中的__autoload
- PHP __autoload机制原理
- PHP __autoload 详解
- PHP __autoload()方法详解
- php中__autoload()
- JS 对类的支持
- 120. Triangle
- 稀有矿井
- oracle导入文件时,日期格式问题
- awk中多个分隔符^识别办法
- 【转载修改】PHP的__autoLoad与spl_autoload_*
- 炮兵阵地
- Hbase和Hive的区别与联系
- 在ListView的后面添加View
- android一些新东西学习博客
- textview的属性总结
- CF 808E. Selling Souvenirs
- 图的存储和遍历
- tensorflow git 包含多种算法模型的实现