Home > php教程 > php手册 > body text

php 普通错误和致命错误捕获及处理

WBOY
Release: 2016-06-06 19:33:02
Original
1064 people have browsed it

无详细内容 无 ?php//禁止错误输出error_reporting(0);//设置错误处理器set_error_handler('errorHandler');register_shutdown_function('fatalErrorHandler');class Test{public function index(){//这里发生一个警告错误,出发errorHandler echo $undefined

<?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,
			 			);
			 //写入错误日志
                         //格式 :  时间 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();
Copy after login
php 普通错误和致命错误捕获及处理
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!