·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> php网站开发 >> php异常处理示例

php异常处理示例

作者:佚名      php网站开发编辑:admin      更新时间:2022-07-23
php异常处理示例php异常处理使用示例,代码说明了普通错误和致命错误捕获及处理的方法。代码如下:<?php//禁止错误输出error_reporting(0);//设置错误处理器set_error_handler('errorHandler');register_shutdown_function('fatalErrorHandler');classTest{publicfunctionindex(){//这里发生一个警告错误,出发errorHandlerecho$undefinedVarible;}}functionerrorHandler($errno,$errstr,$errfile,$errline){$arr=array('['.date('Y-m-dh-i-s').']','http://www.baidu.com','|',$errstr,$errfile,'line:'.$errline,); // www.jbxue.com//写入错误日志//格式:时间uri|错误消息文件位置第几行error_log(implode('',$arr)."\r\n",3,'./test.txt','extra');echoimplode('',$arr)."\r\n";}//捕获fatalErrorfunctionfatalErrorHandler(){$e=error_get_last();switch($e['type']){caseE_ERROR:caseE_PARSE:caseE_CORE_ERROR:caseE_COMPILE_ERROR:caseE_USER_ERROR:errorHandler($e['type'],$e['message'],$e['file'],$e['line']);break;}}$test=newTest();////这里发生一个警告错误,被errorHandler捕获$test->index();//发生致命错误,脚本停止运行触发fatalErrorHandler$test=newTesdt();$test->index();