php异常处理示例,php异常处理示例
php異常處理示例
1.[代碼][PHP]代碼
//禁止錯誤輸出
error_reporting(0);
//設置錯誤處理器
set_error_handler('errorHandler');
register_shutdown_function('fatalErrorHandler');
class Test{
public function index(){
//這里發生一個警告錯誤,出發errorHandler
echo $undefinedVarible;
}
}
function errorHandler($errno,$errstr,$errfile,$errline){
$arr = array(
'['.date('Y-m-d h-i-s').']',
'http://www.baidu.com',
'|',
$errstr,
$errfile,
'line:'.$errline,
); // www.jbxue.com
//寫入錯誤日志
//格式 : 時間 uri | 錯誤消息 文件位置 第幾行
error_log(implode(' ',$arr)."\r\n",3,'./test.txt','extra');
echo implode(' ',$arr)."\r\n";
}
//捕獲fatalError
function fatalErrorHandler(){
$e = error_get_last();
switch($e['type']){
case E_ERROR:
case E_PARSE:
case E_CORE_ERROR:
case E_COMPILE_ERROR:
case E_USER_ERROR:
errorHandler($e['type'],$e['message'],$e['file'],$e['line']);
break;
}
}
$test = new Test();
這里發生一個警告錯誤,被errorHandler 捕獲
$test->index();
//發生致命錯誤,腳本停止運行觸發 fatalErrorHandler
$test = new Tesdt();
$test->index();
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的php异常处理示例,php异常处理示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux增加php进程数,linux
- 下一篇: oracle 输出 三角形,10. 三角