首页 > 解决方案 > 无法设置错误处理程序以在 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-apachedocker 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 上试过了。

标签: phpmacosdockerterminalphp-8

解决方案


正如评论中所指出的,这确实是一个错误,并已在第二个候选版本(PHP-8.0.0 RC2)中得到解决。


推荐阅读