繁体   English   中英

Php,有没有办法将所有通知/警告变成例外?

[英]Php, is there a way to turn all notices/warnings into an exception?

我已经设置了错误处理程序:

set_error_handler (function($errno, $errstr, $errfile,  $errline, array $errcontext) {
  $s = date('Ymd_His');
  switch ($errno)
  {
    case E_USER_ERROR:
      $s.= '_E_';
      break;
    case E_USER_WARNING:
      $s.= '_W_';
      break;
    case E_USER_NOTICE:
      $s.= '_N_';
      break;
    default:
      $s.= '_U_';
      break;
    }
    file_put_contents (APP_PATH_CACHE.'/log'.$s.'_'.rand(1,99999).'.html', print_r(get_defined_vars(), true));
}, E_ALL);

但它可以变成例外吗? 这样我才能看到流动。

是。 这正是ErrorException创建的原因。 http://php.net/manual/en/class.errorexception.php

function exception_error_handler($severity, $message, $file, $line) {
    if (!(error_reporting() & $severity)) {
        // This error code is not included in error_reporting
        return;
    }
    throw new ErrorException($message, 0, $severity, $file, $line);
}
set_error_handler("exception_error_handler");

是的,它可以简单地完成。 最好的方法是创建自己的类,该类从\\ Exception和此新类的error_handler()throw对象中扩展。

您也可以在以后以简单的方式完成

file_put_contents (APP_PATH_CACHE.'/log'.$s.'_'.rand(1,99999).'.html', print_r(get_defined_vars(), true));

throw new Exception($errst, $errno); 其中第一个参数是消息,第二个是错误号

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM