Re: Enable error_handler callback parameters to be passed by reference
| From: | Thomas Bley | Date: | Sat, 31 Jan 2015 17:40:37 +0000 |
| Subject: | Re: Enable error_handler callback parameters to be passed by reference | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-81493@lists.php.net to get a copy of this message | ||
error_log() is a good point. In the future, set_error_handler() might be changed to be called
multiple times with different custom error handlers, similar to how register_shutdown_function() and
spl_autoload_register() act on multiple calls.
Having a chain of error handlers appending data to $errstr makes it difficult to use error_log(),
because this is a one-time operation.
Also, error_log() has the ability to override the "error_log" property from php.ini, which
might not be the desired behaviour.
So I would prefer $errstr to be passed by reference.
For completeness, error_log() currently has no parameters for $errno, $line and $file, so an example
would look like this:
function myErrorHandler($errno, $errstr, $errfile, $errline) {
switch($errno){
case E_WARNING: $errnoStr='Warning'; break;
case E_NOTICE: $errnoStr='Notice'; break;
case E_STRICT: $errnoStr='Strict'; break;
case E_RECOVERABLE_ERROR: $errnoStr='Recoverable Error'; break;
case E_DEPRECATED: $errnoStr='Deprecated'; break;
case E_USER_ERROR: $errnoStr='User Error'; break;
case E_USER_WARNING: $errnoStr='User Warning'; break;
case E_USER_NOTICE: $errnoStr='User Notice'; break;
case E_USER_DEPRECATED: $errnoStr='User Deprecated'; break;
}
if (!empty($_SESSION['username'])) {
$errstr .= ', username: '.$_SESSION['username'];
}
error_log('PHP '.$errnoStr.': '.$errstr.' in '.$errfile.' on
line '.$errline);
return true;
}
Regards
Thomas
Dan Ackroyd wrote on 30.01.2015 10:38:
> On 21 January 2015 at 04:39, Thomas Bley <mails@thomasbley.de> wrote:
>> In userland it is sometimes necessary to extend PHP's notices/warnings with
>> additional information (e.g. username from session, stack trace, etc.)
>
> Why can't you just use in the error handler function to write the
> exact message you want?
>
> http://php.net/manual/en/function.error-log.php
>
> cheers
> Dan
>