是否可以让PHP在全局通知/警告后停止执行?
我们运行一个开发服务器上有很多站点,但是想强迫我们的开发人员修复这些警告/通知(或者至少向他们寻求帮助)而不是忽略并继续前进.
我想知道将所有PHP错误全局转换为异常是否被认为是一种不好的做法.将使用以下内容:
function exception_error_handler($errno, $errstr, $errfile, $errline ) {
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
return false;
}
Run Code Online (Sandbox Code Playgroud)
我想假设您可以开始在通常会抛出错误的某些代码片段周围使用"try/catch".
如果不是好/坏的情况,这种做法会产生什么样的问题呢?
好的,这需要一些时间来分解它。这里是:
有一个包含的错误脚本,它是本文其余部分的以下内容:
<?php
$a = 4 // missing semicolon
$b = 2;
Run Code Online (Sandbox Code Playgroud)
然后考虑以下脚本来处理错误。请注意,最初未注册自定义异常处理程序。
<?php
// disable default display of errors
ini_set('display_errors', 0);
// register functions
#set_exception_handler('catchException'); // initially not set
register_shutdown_function('catchError');
// define error function
function catchError(){
echo "PHP version: ".phpversion();
if(is_null(error_get_last())) echo "<h1>No errors fetched!</h1>";
else echo "<h1>Error fetched:</h1>";
var_dump(error_get_last());
}
// define exception function (not used in all examples)
function catchException(){}
// include faulty script
include("D:/temp/faulty.php");
Run Code Online (Sandbox Code Playgroud)
PHP 5 和 7 的结果是相同的。error_get_last() 函数返回最后发生的错误 ( Screenshot )。 …