有没有办法从PHP中的SPL自动加载器中抛出异常,以防它失败?它似乎不适用于PHP 5.2.11.
class SPLAutoLoader{
public static function autoloadDomain($className) {
if(file_exists('test/'.$className.'.class.php')){
require_once('test/'.$className.'.class.php');
return true;
}
throw new Exception('File not found');
}
} //end class
//start
spl_autoload_register( array('SPLAutoLoader', 'autoloadDomain') );
try{
$domain = new foobarDomain();
}catch(Exception $c){
echo 'File not found';
}
Run Code Online (Sandbox Code Playgroud)
当调用上面的代码时,没有异常的迹象,而是我得到一个标准的"致命错误:类'foobarDomain'在bla中找不到".并且脚本的执行终止.