說明:自動加載類文件到本文件。 我們在用PHP4的時候一般用類的過程應該是這樣的: 類文件加載require(類.php) 或include(類.php) $test = new 類名 然後使用類方法。 php5以後就不用了,因爲PHP5提供了一個簡潔方便的方法,那就是autoload 具體舉例說明: test.php類文件(用與自動加載)
<?PHP
class test{//類開始
function echo_str(){print "this is test files";}
}//類結束
?>
testone.php文件
<?PHP
$a = new test;
$a->echo_str();
function __autoload(strtolower($className)){ //strtolower是自動轉化爲小寫字母(當然你可以不用strtolower,因爲php5會自動將其轉化爲小寫的)
require_once($className.".php"); //自動加載類文件,根據類的名稱給予文件名(即爲加載規則)
}
?>
轉自:http://fuyuanjin.phpx.com/happy/thread-145034-1-1.html