在 PHP 7.4.0 中,我看到以下警告:
Deprecated: Array and string offset access syntax with curly braces is deprecated in ...
我的错误/异常处理程序无法捕获并记录它们。
例子:
<?php
set_error_handler(function ($errNo, $errStr) {
echo "set_error_handler: " . $errStr;
});
set_exception_handler(function ($exception) {
echo "set_exception_handler: " . $exception->getMessage();
});
$b = 'test';
$a = $b{1};
Run Code Online (Sandbox Code Playgroud)
警告仍然显示在正常输出中,并且两个处理程序都没有被调用。
我想在我自己的日志中记录所有错误、异常和警告,但处理程序未捕获此警告。是否有原因或解决方案来捕获和记录 PHP 抱怨的所有内容(无法访问服务器 Apache/PHP 日志)?
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");
}
...
Run Code Online (Sandbox Code Playgroud)
您可能会将其视为“软语法错误”:它是在解析时、编译步骤中发现的,但不是致命的致命错误,而是对未来厄运的警告。与语法错误一样,您有两种处理方式:前置和关闭处理。
$ 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
}
Run Code Online (Sandbox Code Playgroud)
register_shutdown_function(fn() => var_dump(error_get_last()));
$b = 'test';
$a = $b{1};
Run Code Online (Sandbox Code Playgroud)
输出:
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)
}
Run Code Online (Sandbox Code Playgroud)
根据您的需要选择一个或两个。就个人而言,我使用 prepend 方法,因为它可以处理各种其他白屏死机场景。如果您在 Web 上下文中执行此操作,则需要安排您的 Web 服务器来设置auto_prepend_file设置:一旦您的“主要”代码运行,您就无法设置前置并使其按此处所示工作。