弃用警告在PHP 7.4中不可捕获

问题描述 投票:2回答:1

在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};

警告仍显示在正常输出中,并且两个处理程序均未调用。

我想将所有错误,异常和警告记录在我自己的日志中,但是处理程序未捕获此警告。是否有理由或解决方案来捕获和记录PHP抱怨的所有内容(无法访问服务器Apache / PHP日志)?

php php-7.4
1个回答
0
投票

set_error_handler根据文档捕获运行时发出的消息:

此功能可用于定义您自己的运行时错误处理方式

您看到的弃用警告是set_error_handler,根据定义,它发生在运行时之前:

implemented at 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};

使用哪个?

根据您的需要选择一个或两个。就个人而言,我使用前置方法,因为它可以处理其他各种“死亡白屏”方案。如果在Web上下文中执行此操作,则需要安排Web服务器以设置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) } 设置:“主”代码运行后,您将无法设置前缀并使它如此处所示运行。 >

© www.soinside.com 2019 - 2024. All rights reserved.