set_error_handler
根据文档捕获在运行时发出的消息:
此函数可用于定义您自己在运行时处理错误的方式
您看到的弃用警告是在 compile-time 实现的,根据定义,它发生在运行时之前:
static zend_op *zend_delayed_compile_dim(znode *result, zend_ast *ast, uint32_t type) /* {{{ */
{
if (ast->attr == ZEND_DIM_ALTERNATIVE_SYNTAX) {
zend_error(E_DEPRECATED, "Array and string offset access syntax with curly braces is deprecated");
}
...
你可能会认为它是一个“软语法错误”:它是在解析时发现的,在编译步骤中,但它不是一个硬性的致命错误,而是对未来厄运的警告。与语法错误一样,您有两种处理方式:前置处理和关闭处理。
前置
$ cat error-handler.php
<?php
set_error_handler(fn(...$args) => var_dump($args));
$ cat try.php
<?php
$b = 'test';
$a = $b{1};
$ php -d auto_prepend_file=error-handler.php try.php
array(5) {
[0]=>
int(8192)
[1]=>
string(69) "Array and string offset access syntax with curly braces is deprecated"
[2]=>
string(21) "/Users/bishop/try.php"
[3]=>
int(3)
[4]=>
NULL
}
关机处理
register_shutdown_function(fn() => var_dump(error_get_last()));
$b = 'test';
$a = $b{1};
输出:
array(4) {
["type"]=>
int(8192)
["message"]=>
string(69) "Array and string offset access syntax with curly braces is deprecated"
["file"]=>
string(9) "/in/212CF"
["line"]=>
int(7)
}
使用哪个?
根据您的需要选择一个或两个。就个人而言,我使用 prepend 方法,因为它处理各种其他白屏死机场景。如果您在 Web 上下文中执行此操作,则需要安排您的 Web 服务器来设置auto_prepend_file
设置:一旦您的“主”代码运行,您就不能设置 prepend 并让它像这里演示的那样工作。