Why don't php deprecated features (as opposed to deprecated functions) call the error_handler function?
Asked Answered
H

1

1

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. Assigning the return value of new by reference is now deprecated.
  2. 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

Hullo answered 13/9, 2011 at 18:35 Comment(2)
Just a guess: Probably because both are not functions, but language construct.Arboreous
I noticed that distinction, but it still seems odd that I can access the error information by calling error_get_last(). That would indicate that it is being logged as an error like any other. And it has the err_num of E_DEPRECATED.Hullo
N
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)
-----------------------------------
Navy answered 17/12, 2012 at 15:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.