如何在E_NOTICE上使php退出? [英] how to make php exit on E_NOTICE?

查看:115
本文介绍了如何在E_NOTICE上使php退出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常php脚本在E_NOTICE之后继续运行,是否有一种方法可以在函数的上下文中将其提升为致命错误,即只需要在函数中退出,而不是在核心PHP函数上退出,那就是

Normally php script continues to run after E_NOTICE, is there a way to elevate this to fatal error in context of a function, that is I need only to exit on notice only in my functions but not on core php functions, that is globally.

推荐答案

您可以创建一个自定义错误处理程序来捕获 E_NOTICE

You could create a custom error handler to catch E_NOTICEs.

这是未经测试的,但应进入正确的方向:

This is untested but should go into the right direction:

function myErrorHandler($errno, $errstr, $errfile, $errline)
 {
  if ($errno == E_USER_NOTICE)
   die ("Fatal notice");
  else
   return false; // Leave everything else to PHP's error handling

 }

然后,设置它作为新的自定义错误处理程序使用 set_error_handler() 输入您的函数,并在离开时恢复PHP的错误处理程序:

then, set it as the new custom error handler using set_error_handler() when entering your function, and restore PHP's error handler when leaving it:

function some_function()
{

// Set your error handler
$old_error_handler = set_error_handler("myErrorHandler");

... do your stuff ....

// Restore old error handler
set_error_handler($old_error_handler);

}

这篇关于如何在E_NOTICE上使php退出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆