Home  >  Article  >  php教程  >  php异常处理示例

php异常处理示例

PHP中文网
PHP中文网Original
2016-05-26 08:18:391137browse

跳至

//禁止错误输出
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();

                   

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn