为什么php不赞成使用的功能(与不赞成使用的功能相反)不调用error_handler函数? [英] Why don't php deprecated features (as opposed to deprecated functions) call the error_handler function?

查看:95
本文介绍了为什么php不赞成使用的功能(与不赞成使用的功能相反)不调用error_handler函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am upgrading a codebase from php 5.2 to 5.3. As part of this I am converting our uses of deprecated functions and features. When we use deprecated functions like split and spliti the error handler that we setup by calling set_error_handler() is called and I get a log message. This is great.

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

But, when I use the following two deprecated features:

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

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

The error handler is not called so I do not see a log message. If I call error_get_last() I see that the error is logged and I can also see it in the php error log, but we use the error handler to catch all of these. I'm concerned that something in my server setup is causing something to not work correctly.

您可以在此处查看已弃用的功能: http://www. php.net/manual/en/migration53.deprecated.php

You can see the deprecated features/functions here: http://www.php.net/manual/en/migration53.deprecated.php

推荐答案

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

You can trace the deprecated error messages as well with set_error_handler(). The problem you describe is, that these deprication messages are given before you have registered your error handling function.

您命名的两条消息在解析时给出.这意味着如果您注册错误处理函数的时间太晚,您将无法再对其进行处理(因为它们已通过).

The two messages you name are given at parse-time. That means if you register your error handler function too late, you can not handle them any longer (because they have passed).

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

The solution therefore is trivial: Register your error handler before these files are parsed. Working Example:

文件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会产生以下输出,因为您可以看到错误处理程序正在处理所有其他其他错误旁边已弃用的消息:

Running php error-handler-deprecated.php under PHP 5.3 then produces the following output, as you can see the error handler is handling all those deprecated messages next to the other errors:

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不赞成使用的功能(与不赞成使用的功能相反)不调用error_handler函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆