php異常處理使用示例
<?php
//禁止錯誤輸出
error_reporting(0);
//設(shè)置錯誤處理器
set_error_handler('errorHandler');
register_shutdown_function('fatalErrorHandler');
class Test{
public function index(){
//這里發(fā)生一個警告錯誤,出發(fā)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,
);
//寫入錯誤日志
//格式 : 時間 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();
////這里發(fā)生一個警告錯誤,被errorHandler 捕獲
$test->index();
//發(fā)生致命錯誤,腳本停止運行觸發(fā) fatalErrorHandler
$test = new Tesdt();
$test->index();
![]() |
相關(guān)文章
thinkphp5+layui實現(xiàn)的分頁樣式示例
這篇文章主要介紹了thinkphp5+layui實現(xiàn)的分頁樣式,結(jié)合實例形式詳細(xì)分析了thinkPHP5框架結(jié)合layui實現(xiàn)的分頁功能相關(guān)的配置、查詢等操作技巧,需要的朋友可以參考下2019-10-10
Win7環(huán)境下Apache連接MySQL提示連接已重置的解決辦法
這篇文章主要為大家詳細(xì)介紹了Win7環(huán)境下Apache連接MySQL提示“連接已重置”的解決辦法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05
利用phpexcel把excel導(dǎo)入數(shù)據(jù)庫和數(shù)據(jù)庫導(dǎo)出excel實現(xiàn)
本文介紹利用phpexcel對數(shù)據(jù)庫數(shù)據(jù)導(dǎo)入excel(excel篩選)、導(dǎo)出excel,大家參考使用吧2014-01-01


