spl_autoload_register和__autoload
2015-10-09 11:12
190 查看
1.实例化一个[b]未定义的类时会触发[/b]
2.类存在继承关系时,被继承的类没有引入的情况下,会触发 (继承关系的两个类必须在同一个目录下)
__autoload
实例化PRINTIT类,'PRINTIT'作为参数,传递到__autoload()中,并作为文件名称加载
spl_autoload_register
spl_autoload_register 这种方法提供了更灵活的方式去注册加载器。根据场景的不同,定义不同的加载函数,再依据条件分别注册成加载器。
加载器为函数
加载器为对象方法
2.类存在继承关系时,被继承的类没有引入的情况下,会触发 (继承关系的两个类必须在同一个目录下)
__autoload
实例化PRINTIT类,'PRINTIT'作为参数,传递到__autoload()中,并作为文件名称加载
function __autoload( $class ) { $file = $class . '.class.php'; if ( is_file($file) ) { require_once('INCPATH'.$file); } } $obj = new PRINTIT(); $obj->doPrint();
spl_autoload_register
spl_autoload_register 这种方法提供了更灵活的方式去注册加载器。根据场景的不同,定义不同的加载函数,再依据条件分别注册成加载器。
加载器为函数
function loadprint( $class ) { $file = $class . '.class.php'; if (is_file($file)) { require_once($file); } } spl_autoload_register( 'loadprint' ); $obj = new PRINTIT(); $obj->doPrint();
加载器为对象方法
class test { public static function loadprint( $class ) { $file = $class . '.class.php'; if (is_file($file)) { require_once($file); } } } spl_autoload_register( array('test','loadprint') ); //另一种写法:spl_autoload_register( "test::loadprint" ); $obj = new PRINTIT(); $obj->doPrint();
相关文章推荐
- 深度理解链式前向星
- 字节流实现文件和文件夹的拷贝
- Google Chart API学习(一)
- [Leetcode] Longest Consecutive Sequence
- spark源码学习系列的转载博文
- 003-storm设计思想和处理过程
- 20.OC语言构造方法
- Jenkins系列之—19 Jenkins语言本地化
- MIT 操作系统实验 MIT JOS lab2
- Mysql导入导出表数据
- yii2URL美化
- gcc 编译器常用的命令行参数一览
- 1012. The Best Rank (25)
- myeclipse 下 fineReport 报表制作流程简介
- JS实现黑色风格的网页TAB选项卡效果代码
- request和response
- unity导出安卓APK后图片错乱的解决办法--getResources().getIdentifier()的使用
- Git命令 更新
- 程序员的权利—兄弟连IT教育
- Fourier Series Intro - Laplace Series