2013-04-03 / JSN / 0 Comments / 33 次瀏覽
<?php
session_start();
$_SESSION['test'] = 1;
function __autoload($className) {
$className = $className . '.php';
include_once($className);
echo "因為 $className 不存在,在掛掉之前執行了加載了, 如果有成功就往下執行,反之則會終止程式 <br>";
}
$testclass1 = new testclass1();
$testclass2 = new testclass2();
echo "END";
?>
輸出會是
因為 testclass1.php 不存在,在掛掉之前執行了加載了, 如果有成功就往下執行,反之則會終止程式
因為 testclass2.php 不存在,在掛掉之前執行了加載了, 如果有成功就往下執行,反之則會終止程式
END
用法就在於,當class不存在時,會自動呼叫__autoload(), 如果到__autoload()結束前都沒辦法找到class來源,那就會終止執行(die;)。