PHP文件自動加載
<?php
/*定義系統(tǒng)默認(rèn)的自動加載函數(shù)*/
function __autoload($className){
if(file_exists($className.'.php')){
require_once($className.'.php');
}
}
// 注冊自己的自動加載函數(shù)
//function autoload(){
// if(file_exists('Test.php')){
// require_once('Test.php');
// }
//}
//
//spl_autoload_register('autoload');
// 類方式的實(shí)現(xiàn),注意加載函數(shù)必須是靜態(tài)函數(shù)
//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中所有的控制及業(yè)務(wù)代碼都用類的方式定義,那么將不需要進(jìn)行任何的頁面包含
// 將十分方便,而且不容易出錯
// 你必須保證你的(PHP 5 >= 5.1.2)才能使用
?>
posted on 2011-10-08 18:06 vagasnail 閱讀(655) 評論(0) 編輯 收藏 所屬分類: 動態(tài)語言 、php