php 设置统一处理错误,统一的PHP错误处理理论
我建議去“異常”的方式。
當有用戶錯誤時拋出異常,您可以將php錯誤轉換為異常,如下所示:
function exception_error_handler($errno, $errstr, $errfile, $errline ) {
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}
set_error_handler("exception_error_handler");
盡管這種行為在OOP環境中效果最好。如果您沒有單個入口點(如前臺控制器),您也可能會遇到松散的異常:
function myException($exception)
{
echo "Exception: " , $exception->getMessage();
}
set_exception_handler('myException');
簡單的調試與異常會有點像這樣:
function parseException($e) {
$result = 'Exception: "';
$result .= $e->getMessage();
$trace = $e->getTrace();
foreach (range(0, 10) as $i) {
$result .= '" @ ';
if (!isset($trace[$i])) {
break;
}
if (isset($trace[$i]['class'])) {
$result .= $trace[$i]['class'];
$result .= '->';
}
$result .= $trace[$i]['function'];
$result .= '(); ';
$result .= $e->getFile() . ':' . $e->getLine() . "\n\n";
}
return $result;
}
從那里評估全球變化等是在公園散步。您可能會為Symfony Framework Debug Toolbar尋找靈感,該工具欄提供了許多這些請求。
總結
以上是生活随笔為你收集整理的php 设置统一处理错误,统一的PHP错误处理理论的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 上海设计备案流程(上海设计备案)
- 下一篇: php static与self,PHP5