我正在尝试找出PHP中处理错误的更好方法。如果parse_ini_file
调用存在问题,我想抛出异常。这种方法可以运行,但是否有更优雅的方式来处理错误呢?
public static function loadConfig($file, $type)
{
if (!file_exists($file))
{
require_once 'Asra/Core/Exception.php';
throw new Asra_Core_Exception("{$type} file was not present at specified location: {$file}");
}
// -- clear the error
self::$__error = null;
// -- set the error handler function temporarily
set_error_handler(array('Asra_Core_Loader', '__loadConfigError'));
// -- do the parse
$parse = parse_ini_file($file, true);
// -- restore handler
restore_error_handler();
if (!is_array($parse) || is_null($parse) || !is_null(self::$__error))
{
require_once 'Asra/Core/Exception.php';
throw new Asra_Core_Exception("{$type} file at {$file} appears to be
}
}
loadConfigError
函数只是将 __error
设置为错误字符串:
private static function __loadConfigError($errno, $errstr, $errfile, $errline)
{
self::$__error = $errstr;
}
谢谢!