PHP __autoload与spl_autoload
来源:互联网 发布:淘宝子账号可以开店吗 编辑:程序博客网 时间:2024/06/02 13:05
__autoload的最大缺陷是无法有多个autoload方法。
好了, 想想下面的这个情景,你的项目引用了别人的一个项目,你的项目中有一个__autoload,别人的项目也有一个__autoload,这样两个__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)
实际项目中,通常方法如下(当类找不到的时候才会被调用):
// Example to auto-load class files from multiple directories using the SPL_AUTOLOAD_REGISTER method. // It auto-loads any file it finds starting with class.<classname>.php (LOWERCASE), eg: class.from.php, class.db.php spl_autoload_register(function($class_name) { // Define an array of directories in the order of their priority to iterate through. $dirs = array( 'project/', // Project specific classes (+Core Overrides) 'classes/', // Core classes example 'tests/', // Unit test classes, if using PHP-Unit ); // Looping through each directory to load all the class files. It will only require a file once. // If it finds the same class in a directory later on, IT WILL IGNORE IT! Because of that require once! foreach( $dirs as $dir ) { if (file_exists($dir.'class.'.strtolower($class_name).'.php')) { require_once($dir.'class.'.strtolower($class_name).'.php'); return; } } });
0 0
- PHP __autoload与spl_autoload
- 深入理解 spl_autoload , __autoload 以及 spl_autoload_register
- php中spl_autoload详解
- PHP php中spl_autoload详解
- php __autoload
- php: __autoload 与 sql_autoload_register 自动加载区别
- 【转载修改】PHP的__autoLoad与spl_autoload_*
- PHP spl_autoload和class_exsits使用技能
- PHP进阶学习之spl_autoload_register与__autoload方法使用
- php __autoload()函数与require 和include() 函数的介绍
- PHP函数__autoload失效原因(与smarty有关)
- PHP中的__autoload
- PHP __autoload机制原理
- PHP __autoload 详解
- PHP __autoload()方法详解
- php中__autoload()
- php魔术方法__autoload()
- php 魔法函数 __autoload()
- iOS 【CocoaPods 全攻略】
- 个人笔记 js 14 js实现点击单元格行跳转链接到指定页面
- 指针和地址学习
- 数据结构学习地址--上海交大acm试点班总教头俞勇老师讲的哦,还有其他课程地址,超全icourse
- poj2362 dfs 剪枝 比水题难的题
- PHP __autoload与spl_autoload
- Intent之实现功能导航
- AlertDialog 用法大全
- 安卓开发——TypedValue.applyDimension()
- #学习笔记#(31)正则表达式全部符号解释
- android程序的断点单步调试步骤
- hdu1811 Rank of Tetris(待解决 拓扑排序)
- STM32Cube MicroXplorer
- 来年春天