Home  >  Article  >  Backend Development  >  PHP exception handling usage example_PHP tutorial

PHP exception handling usage example_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:36:52844browse

Copy code The code is as follows:

//Disable error output
error_reporting(0 );
//Set error handler
set_error_handler('errorHandler');
register_shutdown_function('fatalErrorHandler');
class Test{
public function index(){
/ /A warning error occurs here, start 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,
);
//Write error log
//Format: Time uri | Error message file location line number
error_log(implode(' ',$arr)."rn",3,'./test.txt','extra');
echo implode(' ',$arr)."rn";
}

//Capture 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();
////A warning error occurred here and was captured by errorHandler
$test->index();
//A fatal error occurs and the script stops running and triggers fatalErrorHandler
$test = new Tesdt();
$test->index();



PHP exception handling usage example_PHP tutorial
 

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/736856.htmlTechArticleCopy the code as follows: ?php //Disable error output error_reporting(0); //Set the error handler set_error_handler ('errorHandler'); register_shutdown_function('fatalErrorHandler');...
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