问题
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:
- Assigning the return value of new by reference is now deprecated.
- Call-time pass-by-reference is now deprecated.
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.
You can see the deprecated features/functions here: http://www.php.net/manual/en/migration53.deprecated.php
回答1:
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:
File 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);
File 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');
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)
-----------------------------------
来源:https://stackoverflow.com/questions/7406737/why-dont-php-deprecated-features-as-opposed-to-deprecated-functions-call-the