为了某种目的而需要捕获系统错误,在此简单分享下某个项目中的捕获方法。
默认php错误如图
index.php
/** * 捕获应用错误记录 */function ef_error_record($errno,$errstr,$errfile,$errline){ $e = array(no=>$errno,str=>$errstr,file=>$errfile,line=>$errline); ef_error_display(record,$e);}/** * 错误中断显示 */function ef_error_display($method='',$option=''){ static $e = array(); if($method=='record'){ $e[] = $option; return true; }else if(!empty($e)){ include ('/common/template/error.php'); exit(); } return true;} //禁止错误输出 error_reporting(0); //设置自定义错误函数 捕获系统错误并记录 set_error_handler(ef_error_record); //注册结束脚本函数 该函数会有意外或正常结束脚本触发(同析构函数) register_shutdown_function(ef_error_display);
error.php
eframe 运行错误 eframe 运行错误 $val){ echo [.($key+1).] {$val['str']}
, .$val['file']. 行: {$val['line']}
; } ?>
最终错误错误页面(当然页面可以自己修改想要的样式):