php - 为什么php不赞成使用的功能(相对于反对使用的功能)不调用error_handler函数?

标签 php error-handling php-5.3

我正在将代码库从php 5.2升级到5.3。作为此过程的一部分,我将转换对不赞成使用的功能和特性的使用。当我们使用不赞成使用的功能(例如split和spliti)时,将调用通过调用 set_error_handler() 设置的错误处理程序,并且会收到一条日志消息。这很棒。

但是,当我使用以下两个不推荐使用的功能时:

  • 现在不建议通过引用分配new的返回值。
  • 现在不建议使用调用时传递引用。

  • 错误处理程序未调用,因此我看不到日志消息。如果我调用 error_get_last() ,我看到错误已记录,并且我也可以在php错误日志中看到它,但是我们使用错误处理程序来捕获所有这些错误。我担心服务器设置中的某些内容导致某些内容无法正常工作。

    您可以在此处查看不推荐使用的功能:http://www.php.net/manual/en/migration53.deprecated.php

    最佳答案

    您还可以使用set_error_handler()跟踪已弃用的错误消息。您描述的问题是,在注册错误处理功能之前会给出这些贬值消息。

    您命名的两条消息在解析时给出。这意味着如果您注册错误处理程序函数太晚,则无法再处理它们(因为它们已通过)。

    因此,解决方案很简单:在解析这些文件之前注册您的错误处理程序。工作示例:

    文件error-handler-deprecated-include.php:

    <?php
    
    # 1. Assigning the return value of new by reference is now deprecated.
    $var = &new stdClass();
    
    # 2. Call-time pass-by-reference is now deprecated
    trim(&$var);
    

    文件error-handler-deprecated.php:
    <?php
    
    $handler = function($errno, $errstr, $errfile, $errline) {
        echo "Error: ", var_dump($errno, $errstr, $errfile, $errline), 
             "-----------------------------------\n";
    };
    
    echo 'set_error_handler() [previous handler]: ', 
          var_dump(set_error_handler($handler));
    
    # test 1. and 2. by including the code *after* the error handler has been set
    include('error-handler-deprecated-include.php');
    

    然后在PHP 5.3下运行php error-handler-deprecated.php会产生以下输出,因为您可以看到错误处理程序正在处理所有其他其他错误旁边已弃用的消息:
    set_error_handler() [previous handler]: NULL
    Error: int(8192)
    string(60) "Assigning the return value of new by reference is deprecated"
    string(98) "error-handler-deprecated-include.php"
    int(7)
    -----------------------------------
    Error: int(8192)
    string(47) "Call-time pass-by-reference has been deprecated"
    string(98) "error-handler-deprecated-include.php"
    int(10)
    -----------------------------------
    Error: int(2)
    string(53) "trim() expects parameter 1 to be string, object given"
    string(98) "error-handler-deprecated-include.php"
    int(10)
    -----------------------------------
    

    关于php - 为什么php不赞成使用的功能(相对于反对使用的功能)不调用error_handler函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7406737/

    相关文章:

    php - 为什么字符串在 PHP 5.3 中表现得像数组?

    php - 检查数据库中是否存在电子邮件?

    php - 将base64转换为php音频文件中的声音文件

    Azure数据工厂: Handling inner failure in until/for activity

    php - 调用Python脚本*使用从php导入的非标准库*

    sql - 如何在SQL Server中将nvarchar类型转换为仅时间类型格式

    php - 参数$ tv_sec或$ tv_usec作为参数有什么作用?

    php - Laravel Homestead 'apt-get update' 给出一些 404

    php - 如何删除同一张表中存储的分层父子记录

    javascript - Js setInterval 对于不同的段落不起作用