Allow 'set_error_handler' to handle NULL

Request #60738 Allow 'set_error_handler' to handle NULL
Submitted: 2012-01-13 02:22 UTC Modified: 2012-09-28 04:09 UTC
From: four dot zerooneunauthorized at gmail dot com Assigned: laruence (profile)
Status: Closed Package: Unknown/Other Function
PHP Version: 5.3.9 OS: irrelivant
Private report: No CVE-ID: None

 [2012-01-13 02:22 UTC] four dot zerooneunauthorized at gmail dot com

Description:
------------
Can the 'set_error_handler' function be made to accept NULL as the parameter in such a way as to reset this feature to the default state of -no- handler being set?  This would duplicate the behavior of the 'set_exception_handler' function.

Note: this change should not interfere with the 'restore_error_handler' function.

And as for the 'set_exception_handler' function - if NULL is given as a parameter, can the return value of 'set_exception_handler' be set to the details of the previously set exception handler function (string or array) instead of always '(bool) true' as it now does?

Test script:
---------------
function testhandler1($errno= null, $errstr= null, $errfile= null, $errline = null)
{
	error_log('1: ' . print_r(func_get_args(), true));
	die();
}

function testhandler2($errno= null, $errstr= null, $errfile= null, $errline = null)
{
	error_log('2: ' . print_r(func_get_args(), true));
	die();
}

var_dump(set_error_handler('testhandler1'));
var_dump(set_error_handler('testhandler2'));
var_dump(set_error_handler(null));


Expected result:
----------------
NULL
string(12) "testhandler1"
string(12) "testhandler2"

Actual result:
--------------
On line 'var_dump(set_error_handler(null));', a 'set_error_handler() expects the argument () to be a valid callback' error is generated and handled by function "testhandler2"

Patches

bug60738.patch (last revision 2012-03-24 04:17 UTC by laruence@php.net)
error_handler_patch_1.diff (last revision 2012-03-23 20:50 UTC by nikic@php.net)
set_error_handler-allow-null-parameter-corrected (last revision 2012-03-11 07:46 UTC by kevin dot swinton at gmail dot com)
set_error_handler-allow-null-parameter (last revision 2012-03-10 22:34 UTC by kevin dot swinton at gmail dot com)

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports

 [2012-03-13 08:56 UTC] kevin dot swinton at gmail dot com

In the first instance, nothing in here relates to a bug, rather both are feature requests.

The first request is for set_error_handler() to function in an identical way to set_exception_handler() when called with a single NULL parameter. In the interests of a more consistent API this would seem sensible.

The second request is for set_exception_handler() (and by extension set_error_handler()) to behave in a different way when called with a single NULL parameter.

Given there is no compelling argument, and that such a change could technically break BC, and that the request in itself doesn't appear to achieve anything useful, the suggestion would be that the first request could be implemented whilst the second request has no case.

As a result, I have attached a patch to implement only the first request.

 [2012-03-24 03:26 UTC] laruence@php.net

Hi, I attached another fix, this will save one alloc/free pair, and also save one 
(IS_NULL == Z_TYPE_P(error_handler). 

Nikic,  if you have no objection, I will commit the patch.

thanks

 [2012-03-24 08:52 UTC] stas@php.net

set_error_handler(null) should return old handler, not true - just as any other 
function does.

 [2012-03-24 08:52 UTC] stas@php.net

-Status: Closed +Status: Re-Opened

 [2012-03-24 10:00 UTC] laruence@php.net

As discussion at the pull request:
in the manual of set_exception_handler says:
   " Returns the name of the previously defined exception handler, or
NULL on error. If no previous handler was defined, NULL is also
returned. If NULL is passed, resetting the handler to its default
state, TRUE is returned. "

close this entry, change the return value should be another issue(FR)

 [2012-03-24 11:44 UTC] laruence@php.net

As Stas asked to revert it from 5.4,  so now only implement in trunk.

 [2012-09-28 04:09 UTC] laruence@php.net

The fix for this bug has been committed.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.

 For Windows:

http://windows.php.net/snapshots/
 
Thank you for the report, and for helping us make PHP better.

nikic already commmitted the new implemention.