# Enable error_handler callback parameters to be passed by reference

- Thread: https://externals.io/message/80921
- Original archive: https://news-web.php.net/php.internals/80921
- Started by Thomas Bley on 2015-01-21T04:39:39+00:00
- Messages: 10
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #80921 — Thomas Bley — 2015-01-21T04:39:39+00:00

URL: https://externals.io/message/80921#80921

In userland it is sometimes necessary to extend PHP's notices/warnings with additional information (e.g. username from session, stack trace, etc.)
I'm proposing to enable error_handler callback parameters to be passed by reference to be able to append additional data to error messages.

Example:

<?php
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 0);
ini_set('log_errors', 1);
ini_set('error_log', 'php_error.log');

function myErrorHandler($errno, &$errstr, $errfile, $errline) {
  if (!empty($_SESSION['username'])) {
    $errstr .= ', username: '.$_SESSION['username'];
  }
  return false; // continue normal error handler
}

set_error_handler("myErrorHandler");

function test() {
  echo tests; // Use of undefined constant tests
}

test();

Gives in php_error.log:
[21-Jan-2015 04:32:26 UTC] PHP Notice:  Use of undefined constant tests - assumed 'tests', username: ... in error.php on line 17

Instead of:
[21-Jan-2015 04:32:26 UTC] PHP Notice:  Use of undefined constant tests - assumed 'tests' in error.php on line 17

Regards
Thomas

---

## #80970 — Yasuo Ohgaki — 2015-01-22T10:41:46+00:00

URL: https://externals.io/message/80921#80970 · In reply to: #80921

Hi Thomas,



> In userland it is sometimes necessary to extend PHP's notices/warnings
> with additional information (e.g. username from session, stack trace, etc.)
> I'm proposing to enable error_handler callback parameters to be passed by
> reference to be able to append additional data to error messages.
>
> Example:
>
> <?php
> ini_set('error_reporting', E_ALL);
> ini_set('display_errors', 0);
> ini_set('log_errors', 1);
> ini_set('error_log', 'php_error.log');
>
> function myErrorHandler($errno, &$errstr, $errfile, $errline) {
>   if (!empty($_SESSION['username'])) {
>     $errstr .= ', username: '.$_SESSION['username'];
>   }
>   return false; // continue normal error handler
> }
>
> set_error_handler("myErrorHandler");
>
> function test() {
>   echo tests; // Use of undefined constant tests
> }
>
> test();
>
> Gives in php_error.log:
> [21-Jan-2015 04:32:26 UTC] PHP Notice:  Use of undefined constant tests -
> assumed 'tests', username: ... in error.php on line 17
>
> Instead of:
> [21-Jan-2015 04:32:26 UTC] PHP Notice:  Use of undefined constant tests -
> assumed 'tests' in error.php on line 17
>

This would be very useful for accounting!
If you cannot send pull requests, please file this to bugs.php.net as
request.

Regards,

--
Yasuo Ohgaki
yohgaki@ohgaki.net

---

## #80976 — reeze — 2015-01-22T16:01:27+00:00

URL: https://externals.io/message/80921#80976 · In reply to: #80970

I like the idea, we have the similar scenario, we need to print request log
id to the log,

so I just made a PR to implement this:
https://github.com/php/php-src/pull/1018  the PR is against master,
but I think this could also been in 5.5.



> Hi Thomas,
>
> 
>
> > […]
> etc.)
> > […]
>
> This would be very useful for accounting!
> If you cannot send pull requests, please file this to bugs.php.net as
> request.
>
> Regards,
>
> --
> Yasuo Ohgaki
> yohgaki@ohgaki.net

---

## #81028 — Thomas Bley — 2015-01-23T07:46:27+00:00

URL: https://externals.io/message/80921#81028 · In reply to: #80976

Thanks I'll draft a RFC during the weekend.

Regards
Thomas


reeze wrote on 22.01.2015 17:01:

> I like the idea, we have the similar scenario, we need to print request log id to the log,
> 
> 
> so I just made a PR to implement this: https://github.com/php/php-src/pull/1018 <https://github.com/php/php-src/pull/1018>   the PR is against master,
> 
> but I think this could also been in 5.5.
> 
> 
> 
> > […]

---

## #81031 — Nicolas Grekas — 2015-01-23T08:23:58+00:00

URL: https://externals.io/message/80921#81031 · In reply to: #80921

> function myErrorHandler($errno, &$errstr, $errfile, $errline) {...}
>

It would be awesome to also pass $errfile and $errline by reference. This
would allow mapping "compiled" source to real source code and have
meaningful error file+line information.
By "compiled", I mean e.g. inlined classes (like the bootstrap.cache.php
file in Symfony), or preprocessed sources, etc.

Nicolas

---

## #81037 — reeze — 2015-01-23T09:59:09+00:00

URL: https://externals.io/message/80921#81037 · In reply to: #81031

Yeah, seem other want it it too, so I just updated the PR to allow the
first four parameters been passed by reference.

On 23 January 2015 at 16:23, Nicolas Grekas <nicolas.grekas+php@gmail.com>
wrote:

> > […]
>
> It would be awesome to also pass $errfile and $errline by reference. This
> would allow mapping "compiled" source to real source code and have
> meaningful error file+line information.
> By "compiled", I mean e.g. inlined classes (like the bootstrap.cache.php
> file in Symfony), or preprocessed sources, etc.
>
> Nicolas

---

## #81409 — Dan Ackroyd — 2015-01-30T09:38:28+00:00

URL: https://externals.io/message/80921#81409 · In reply to: #80921

> 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

---

## #81456 — Yasuo Ohgaki — 2015-01-30T22:41:08+00:00

URL: https://externals.io/message/80921#81456 · In reply to: #81409

Hi Dan,



> 
> > […]
> 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


All logs may be written into different log, of course.
However, not all apps write "proper"/"required" logs for _admins_. Here
comes
customized save handler.

Besides poorly written apps, apps log should use completely separated log
system IMO.
i.e. Apps should not raise any errors under normal operations and leave
user error/exception
handler to users(admins).

The reason is to detect fatal code/security/subsystem failure as soon as
possible
and to take action for it. i.e. Since errors should not happen with normal
circumstances,
all uncatched error/exception is fatal that requires immediate action.

It's not mandatory, but it's best practice IMHO.

Regards,

--
Yasuo Ohgaki
yohgaki@ohgaki.net

---

## #81457 — Yasuo Ohgaki — 2015-01-30T22:45:45+00:00

URL: https://externals.io/message/80921#81457 · In reply to: #81409

Hi Dan,



> 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
>

Forgot to mention important part.
It's perfectly valid to use error_log. I just think leaving PHP error log
for fatal
errors is a best practice, in general.

Regards,

--
Yasuo Ohgaki
yohgaki@ohgaki.net

---

## #81493 — Thomas Bley — 2015-01-31T17:40:37+00:00

URL: https://externals.io/message/80921#81493 · In reply to: #81409

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:

> 
> > […]
> 
> 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
