一、__autoload

这是一个自动加载函数,在PHP5中,当我们实例化一个未定义的类时,就会触发此函数。

在index.php中,由于没有包含test.class.php,在实例化printit时,自动调用__autoload函数,参数$class的值即为类名printit,此时test.class.php就被包含进来了。

eg:

test.class.php 

<?php 

class test { 

 function doPrint() {
echo 'hello world';
}
}
?> index.php <?
function __autoload( $class ) {
$file = $class . '.class.php';
if ( is_file($file) ) {
require_once($file);
}
} $obj = new test();
$obj->doPrint();
?>

二、spl_autoload_register()

spl_autoload_register(),这个函数与__autoload有与曲同工之妙,将__autoload换成自定义函数。但是该自定义函数不会像__autoload自动触发,这时spl_autoload_register()就起作用了,它告诉PHP碰到没有定义的类就执行该自定义函数()。

<? 

class test {
public static function myAuto( $class ) {
$file = $class . '.class.php';
if (is_file($file)) {
require_once($file);
}
}
} class myTest {
  function doPrint() {
  echo 'hello world';
}
} spl_autoload_register( array('test',myAuto) ); //另一种写法:spl_autoload_register( "test::myAuto" ); $obj = new myTest ();
$obj->doPrint();
?>
 

最新文章

  1. spring-boot 和 docker 集成
  2. [原创]首次制作JQueryUI插件-Timeline时间轴
  3. [转]C#如何把文件夹压缩打包然后下载
  4. Hibernate saveOrUpdate方法到底是怎么执行的?
  5. Oracle权限和数据类型
  6. SQL2012新特性一次一个数据块----特殊的查询分页
  7. 转:Eclipse常用开发插件
  8. php curl post
  9. poj3321Apple Tree(树状数组)
  10. PHP开发APP接口(二)
  11. 初始Android-配置环境
  12. 初入ubuntu
  13. bzoj 4310: 跳蚤
  14. LOJ116 - 有源汇有上下界最大流
  15. 3. [leetcode] Longest Substring Without Repeating Characters
  16. letCode-1
  17. Django之Django终端打印SQL语句
  18. 【hexo】02完成本地创建
  19. vue 关于npm run build 的小问题
  20. 背水一战 Windows 10 (42) - 控件(导航类): Frame 动画

热门文章

  1. Google Colab调用cv2.imshow奔溃
  2. maven项目在myeclipse中不出现Maven Dependencies 和maven标识的解决方法
  3. 【Qt】2.2 继续了解信号和槽
  4. Window命令行杀进程
  5. 学习笔记之30个常用的maven命令
  6. Fortran学习笔记6(函数、子程序)
  7. [LUOGU] P2543 [AHOI2004]奇怪的字符串
  8. js获取移动端触摸坐标
  9. verilog behavioral modeling--sequential and parallel statements
  10. 学习Gulp过程中遇到的一些单词含义