欢迎使用我的 在线工具

小D

读历史、看小说、写程序都是我所爱。技术不好,头脑不灵光,靠的是兴趣。
随笔 - 35, 文章 - 25, 评论 - 13, 引用 - 0
数据加载中……

PHP文件自动加载

<?php

/*定义系统默认的自动加载函数*/
function __autoload($className){
 if(file_exists($className.'.php')){
  require_once($className.'.php');
 }
}

// 注册自己的自动加载函数
//function autoload(){
//    if(file_exists('Test.php')){
//     require_once('Test.php');
//    }
//}
//
//spl_autoload_register('autoload');

// 类方式的实现,注意加载函数必须是静态函数
//class Myloader{
// static function autoload(){
//     if(file_exists('Test.php')){
//      require_once('Test.php');
//     }
//    }
// 
//}
//
//spl_autoload_register(array('Myloader', 'autoload'));


$test = new Test();
$test2 = new Test2();
// 在一个文件中定义多个类也是可以自动加载的


$my = new MyClass('李德成','女');
$my->personInfo();


// 如果我将PHP中所有的控制及业务代码都用类的方式定义,那么将不需要进行任何的页面包含
// 将十分方便,而且不容易出错
// 你必须保证你的(PHP 5 >= 5.1.2)才能使用
?>

posted on 2011-10-08 18:06 vagasnail 阅读(639) 评论(0)  编辑  收藏 所属分类: 动态语言php


只有注册用户登录后才能发表评论。


网站导航: