rfc:error_handler_callback_parameters_passed_by_reference

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
rfc:error_handler_callback_parameters_passed_by_reference [2015/01/27 01:47] – created reezerfc:error_handler_callback_parameters_passed_by_reference [2017/09/22 13:28] (current) – external edit 127.0.0.1
Line 1: Line 1:
 ====== PHP RFC: Allow error_handler callback parameters to be passed by reference ====== ====== PHP RFC: Allow error_handler callback parameters to be passed by reference ======
-  * Version: 0.1 +  * Version: 0.2 
-  * Date: 2015-01-26 (initial draft)+  * Date: 2015-02-28
   * Authors: Reeze Xia <reeze@php.net>, Thomas Bley <mails@thomasbley.de>   * Authors: Reeze Xia <reeze@php.net>, Thomas Bley <mails@thomasbley.de>
-  * Status: Draft+  * Status: Declined
   * First Published at: http://wiki.php.net/rfc/error_handler_callback_parameters_passed_by_reference   * First Published at: http://wiki.php.net/rfc/error_handler_callback_parameters_passed_by_reference
  
Line 17: Line 17:
 We are proposing to enable error_handler callback parameters to be passed by We are proposing to enable error_handler callback parameters to be passed by
 reference to be able to append additional data to error messages. reference to be able to append additional data to error messages.
-This includes the first four parameters ($errno, $errstr, $errfile, $errline) OR only $errstr (depending on your vote)+ 
 +This includes allow: 
 + 
 +1. The first four parameters ($errno, $errstr, $errfile, $errline)  CAN be passed by reference. 
 + 
 +2. OR only allow $errstr parameter be reference. (depending on your vote)
  
 Examples: Examples:
  
-1. Add username from $_SESSION to error_log:+1. Add username from $_SESSION to error_log to help accounting or debugging:
  
 <file php test1.php> <file php test1.php>
Line 45: Line 50:
  
 gives: gives:
-PHP Notice:  Use of undefined constant tests - assumed 'tests', username: john in /tmp/test1.php on line 18+<code>PHP Notice:  Use of undefined constant tests - assumed 'tests', username: john in /tmp/test1.php on line 18</code>
  
 instead of: instead of:
-PHP Notice:  Use of undefined constant tests - assumed 'tests' in /tmp/test1.php on line 18+<code>PHP Notice:  Use of undefined constant tests - assumed 'tests' in /tmp/test1.php on line 18</code>
  
  
-2. Add stack trace to error_log:+2. Add stack trace to error_log (without xdebug):
  
 <file php test2.php> <file php test2.php>
Line 78: Line 83:
  
 gives: gives:
 +<code>
 PHP Notice:  exception 'ErrorException' with message 'Use of undefined constant tests - assumed 'tests'' in /tmp/test2.php:19 PHP Notice:  exception 'ErrorException' with message 'Use of undefined constant tests - assumed 'tests'' in /tmp/test2.php:19
 Stack trace: Stack trace:
Line 84: Line 90:
 #2 /tmp/test2.php(21): test('foo') #2 /tmp/test2.php(21): test('foo')
 #3 {main} in /tmp/test2.php on line 19 #3 {main} in /tmp/test2.php on line 19
 +</code>
  
 instead of: instead of:
 +<code>
 PHP Notice:  Use of undefined constant tests - assumed 'tests' in /tmp/test2.php on line 19 PHP Notice:  Use of undefined constant tests - assumed 'tests' in /tmp/test2.php on line 19
 +</code>
  
  
Line 120: Line 129:
  
 gives: gives:
 +<code>
 PHP Notice:  [wiki.php.net] Use of undefined constant tests - assumed 'tests' PHP Notice:  [wiki.php.net] Use of undefined constant tests - assumed 'tests'
   Request-Uri: /rfc/error_handler_callback_parameters_passed_by_reference   Request-Uri: /rfc/error_handler_callback_parameters_passed_by_reference
   Request-Params: {"do":"edit"}   Request-Params: {"do":"edit"}
   in /tmp/test3.php on line 25   in /tmp/test3.php on line 25
 +</code>
  
 instead of: instead of:
 +<code>
 PHP Notice:  Use of undefined constant tests - assumed 'tests' in /tmp/test3.php on line 25 PHP Notice:  Use of undefined constant tests - assumed 'tests' in /tmp/test3.php on line 25
 +</code>
  
  
Line 134: Line 146:
 Current versions of php.net allow passing parameters for a custom error handler by reference (without warnings), but they don't have any effect. Current versions of php.net allow passing parameters for a custom error handler by reference (without warnings), but they don't have any effect.
 Therefore, framework maintainers can use error handler callback parameters by reference without any problems in older php versions. This change is fully backward compatible. Therefore, framework maintainers can use error handler callback parameters by reference without any problems in older php versions. This change is fully backward compatible.
 +
 +All logged errors are still limited by log_errors_max_len (default 1024 bytes).
  
 ===== Proposed PHP Version(s) ===== ===== Proposed PHP Version(s) =====
Line 158: Line 172:
 ==== Performance ==== ==== Performance ====
  
-TODO+Performance is not affected in normal code execution. In case of an error that can be handled in user-space, we need to copy the referenced variables back to their origins.
  
 ===== Open Issues ===== ===== Open Issues =====
Line 181: Line 195:
  
 gives: gives:
 +<code>
 PHP Notice:  Use of undefined constant tests - assumed 'tests'foo in /tmp/test_binary_safe.php on line 13 PHP Notice:  Use of undefined constant tests - assumed 'tests'foo in /tmp/test_binary_safe.php on line 13
 +</code>
  
 instead of: instead of:
 +<code>
 PHP Notice:  Use of undefined constant tests - assumed 'tests'foobar in /tmp/test_binary_safe.php on line 13 PHP Notice:  Use of undefined constant tests - assumed 'tests'foobar in /tmp/test_binary_safe.php on line 13
 +</code>
  
 +This issue is not related to this rfc and handled separately on https://bugs.php.net/bug.php?id=68963
  
 2. Callback parameter errstr can be changed to an empty string, example: 2. Callback parameter errstr can be changed to an empty string, example:
Line 206: Line 225:
  
 gives: gives:
 +<code>
 PHP Notice:   in /tmp/test_empty_errstr.php on line 13 PHP Notice:   in /tmp/test_empty_errstr.php on line 13
 +</code>
  
 +This is similar to using the @ operator or returning true in the callback function. It is up to the userland developer to avoid this.
  
 3. Shall we allow $errno, $errstr, $errfile, $errline or only $errstr to be passed by reference? 3. Shall we allow $errno, $errstr, $errfile, $errline or only $errstr to be passed by reference?
 +<code>
 laruence: why only one parameter can be reference, but others not? laruence: why only one parameter can be reference, but others not?
 reeze: I am afraid that modify the lineno and file seems a not good practice reeze: I am afraid that modify the lineno and file seems a not good practice
Line 224: Line 246:
 NicolasGrekas: This would allow mapping "compiled" source to real source code and have meaningful error file+line information. NicolasGrekas: 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.   By "compiled", I mean e.g. inlined classes (like the bootstrap.cache.php file in Symfony), or preprocessed sources, etc.
 +</code>
  
 4. Shall we target the rfc on PHP 5.5 / 5.6 ? 4. Shall we target the rfc on PHP 5.5 / 5.6 ?
Line 230: Line 252:
 Possible, but may not come into major distributions if they stick to special minor releases Possible, but may not come into major distributions if they stick to special minor releases
 (e.g. Ubuntu 14.04: 5.5.9, Ubuntu 14.10: 5.5.12, latest: 5.5.21) (e.g. Ubuntu 14.04: 5.5.9, Ubuntu 14.10: 5.5.12, latest: 5.5.21)
 +
 +5. Why can't you just use the error_log() function to write the exact message you want?
 +
 +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. For completeness, error_log() currently has no parameters for $errno, $line and
 +$file, so an example would look like this:
 +
 +<file php test_error_log.php>
 +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;
 +}
 +</file>
  
 ===== Future Scope ===== ===== Future Scope =====
  
 set_error_handler() callback might be able to handle E_ERROR to be able to append additional information to memory_limit exhaustions (or others). set_error_handler() callback might be able to handle E_ERROR to be able to append additional information to memory_limit exhaustions (or others).
-For example try to analyze and fix: [13-Jan-2015 09:24:37 Europe/Berlin] PHP Fatal error:  Allowed memory size of 209715200 bytes exhausted (tried to allocate 262144 bytes) in /var/www/cake/libs/model/datasources/dbo_source.php on line 419 
  
-===== Proposed Voting Choices =====+For example try to analyze and fix: 
 +<code> 
 + [13-Jan-2015 09:24:37 Europe/Berlin] PHP Fatal error:  Allowed memory size of 209715200 bytes exhausted (tried to allocate 262144 bytes) in /var/www/cake/libs/model/datasources/dbo_source.php on line 419 
 +</code> 
 + 
 +===== Vote ===== 
 + 
 +This RFC requires a 50%+1 majority, meaning the first two choices count as Yes, the third choice counts as No. Voting started on 2015-02-13 and will end on 2015-02-27.
  
-This RFC requires 50%+1 majority. +<doodle title="Allow error_handler callback parameters to be passed by reference" auth="thbley" voteType="single" closed="true"> 
 +   * Allow $errstr parameter to be passed by reference 
 +   * Allow $errno, $errstr, $errfile, $fileno parameter to be passed by reference 
 +   * No, Allow none of the parameter be reference parameter 
 +</doodle>
  
 ===== Patches and Tests ===== ===== Patches and Tests =====
  
-Currently implemented on https://github.com/php/php-src/pull/1018+Currently implemented on https://github.com/php/php-src/pull/1018\\ 
 PR is against master and includes the first four callback parameters to be passed by reference. PR is against master and includes the first four callback parameters to be passed by reference.
 +The PR will be updated to correspond vote if any one of the accepted.
  
 I've used to build on Ubuntu 14.04: I've used to build on Ubuntu 14.04:
Line 259: Line 320:
 ===== References ===== ===== References =====
  
-Discussion on php-internals: http://marc.info/?t=142181539200002&r=1&w=2 +  * Discussion on php-internals: http://marc.info/?t=142181539200002&r=1&w=2 
-Discussion on github: https://github.com/php/php-src/pull/1018+  Discussion on Github: https://github.com/php/php-src/pull/1018
  
 ===== Rejected Features ===== ===== Rejected Features =====
Line 268: Line 329:
 ===== Changelog ===== ===== Changelog =====
  
-v0.1 - Initial draft (thbley)+  * v0.2 - updated open issues (thbley) 
 +  * v0.1 - Initial draft (thbley)
rfc/error_handler_callback_parameters_passed_by_reference.1422323248.txt.gz · Last modified: 2017/09/22 13:28 (external edit)