我有以下函数加载我的图书馆:spl_autoload_register - 如何防止致命错误
function load_class($name) {
$namePath = explode('_', $name);
$filePath = '';
if (is_array($namePath)) {
for ($i=0; $i<sizeof($namePath); $i++) {
$filePath .= $namePath[$i];
if ($i != sizeof($namePath) - 1) $filePath .= '/';
}
} else $filePath = $name;
if (is_file($filePath . '.php')) require_once($filePath . '.php');
else if (is_file($filePath . '.class.php')) require_once($filePath . '.class.php');
else throw new Exception('Unable to load class: ' . $name . '. No such file or a directory. ');
if (!class_exists($name)) throw new Exception('Class: ' . $name . ' doesn\'t exists. ');
}
而自动加载:
function __construct() {
try {
spl_autoload_register(array($this, 'load_class'));
} catch (Exception $e) {
echo $e -> getMessage();
}
}
但可惜的是例外并不能阻止我获得类似的致命错误没有课或没有文件。有没有一种优雅的方式来显示用户我自己的沟通,而不是PHP错误?
http://stackoverflow.com/questions/277224/how-do-i-catch-a-php-fatal-error – Bart