php异常处理使用示例_PHP教程

WBOY
Libérer: 2016-07-13 10:36:52
original
804 Les gens l'ont consulté

复制代码 代码如下:

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



php异常处理使用示例_PHP教程
 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/736856.htmlTechArticle复制代码 代码如下: ?php //禁止错误输出 error_reporting(0); //设置错误处理器 set_error_handler('errorHandler'); register_shutdown_function('fatalErrorHandler');...
Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!