PHPのエラーを例外に変換

やっつけメモとソース

体裁は後で

マニュアル見たらset_error_handlerの第一引数がcallback型だったから思いついただけ。

http://goungoun.dip.jp/app/fswiki/wiki.cgi/devnotebook?page=PHP5%A1%A2PHP%A5%A8%A5%E9%A1%BC%A4%F2%CE%E3%B3%B0%28Exception%29%A4%D8%CA%D1%B4%B9




set_error_handler( array( 'My_Exception', 'throwException' ) );

"Error",
E_WARNING => "Warning",
E_PARSE => "Parsing Error",
E_NOTICE => "Notice",
E_CORE_ERROR => "Core Error",
E_CORE_WARNING => "Core Warning",
E_COMPILE_ERROR => "Compile Error",
E_COMPILE_WARNING => "Compile Warning",
E_USER_ERROR => "User Error",
E_USER_WARNING => "User Warning",
E_USER_NOTICE => "User Notice",
E_STRICT => "Runtime Notice"
);

/**
* 例外処理
*
* @param string $errno
* @param string $errstr
* @param string $errfile
* @param string $errline
*/
public function throwException($errno, $errstr, $errfile, $errline)
{
$errorLevel = self::$_errorLevel;

$add_msg= (string)$errno;

if (isset($errorLevel[$errno])) {
$add_msg = $errorLevel[$errno] . ' : ';
}

$instance = new self( $add_msg . $errstr, $errno );

$instance->file = $errfile;
$instance->line = $errline;

throw $instance;
}
}