php - 无法设置错误处理程序以在 PHP-8 中抛出 ErrorException
问题描述
我的脚本有以下代码:
set_error_handler(function(int $severity, string $message, string $filename, int $lineNumber) : void {
throw new ErrorException($message, 0, $severity, $filename, $lineNumber);
});
function test() : string {
return $test;
}
test();
在 PHP 7.3.11(macOS 10.15.7)上运行脚本按预期工作,并显示以下内容:
致命错误:未捕获的 ErrorException:未定义的变量:在 .../index.php:9 中测试 堆栈跟踪:
#0 .../index.php(9): {closure}(8, 'Undefined varia...', '...', 9, Array)
#1 .../index.php(12): 测试()
#2 {主要}
下一个 TypeError: test() 的返回值必须是字符串类型,在 .../index.php:9 中返回 null 堆栈跟踪:
#0 .../index.php(12): 测试()
#1 {main} 在第 9 行的 .../index.php 中抛出
但是在 PHP-8 ( php:8.0.0rc1-apache
docker image ) 上,它卡在一个循环中并且没有显示任何内容。
虽然删除set_error_handler
作品
警告:第 9 行 .../index.php 中未定义的变量 $test
致命错误:未捕获的 TypeError:test():返回值必须是字符串类型,在 .../index.php:9 中返回 null
堆栈跟踪:
#0 .../index.php(12): 测试()
#1 {main} 在第 9 行的 .../index.php 中抛出
这是一个错误,还是在新版本中发生了一些变化?我在 PHP CLI 上试过了。
解决方案
正如评论中所指出的,这确实是一个错误,并已在第二个候选版本(PHP-8.0.0 RC2)中得到解决。
推荐阅读
- javascript - 无法让机器人自动禁止 Discord 上的新帐户
- opencv - 错误:gobject-introspection:未初始化的常量 Language::Python::Shebang
- c - 在 C 中找不到内存泄漏
- html - CSS 复杂网格自动布局
- javascript - 如何使用 json 数据填充表
- javascript - 在 Gatsby.js 中使用 Emotion 的问题
- html - 如何摆脱重叠
- javascript - javascript中clearInterval()的问题
- c++ - 为什么在此示例中无法创建默认移动构造函数
- deep-learning - 使用 CNN + LSTM 识别暴力的训练准确性没有提高