note 120879 added to function.restore-error-handler
| From: | masterada at gmail dot com | Date: | Mon, 27 Mar 2017 13:45:05 +0000 |
| Subject: | note 120879 added to function.restore-error-handler | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-208950@lists.php.net to get a copy of this message | ||
Calling restore_error_handler from within an error handler might result in unexpected behaviour:
<?php
error_reporting(0);
set_error_handler('handleError1');
trigger_error('1-stack:h1');
set_error_handler('handleError2');
trigger_error('2-stack:h1,h2');
trigger_error('6-stack:h1,h2');
trigger_error('7-stack:h1,h2');
function handleError1($code, $message, $file = '', $line = 0, $context = array())
{
echo __METHOD__ . ' ' . $message . PHP_EOL;
}
function handleError2($code, $message, $file = '', $line = 0, $context = array())
{
trigger_error('3-DEFAULT'); // This will use the php's default error handler
echo __METHOD__ . ' ' . $message . PHP_EOL;
set_error_handler('handleError3');
trigger_error('4-stack:h1,h2,h3');
restore_error_handler(); // This will restore the handleError1 instead of the default error handler
trigger_error('5-DEFAULT');
}
function handleError3($code, $message, $file = '', $line = 0, $context = array())
{
echo __METHOD__ . ' ' . $message . PHP_EOL;
}
?>
The above code will output:
handleError1 1-stack:h1
handleError2 2-stack:h1,h2
handleError3 4-stack:h1,h2,h3
handleError1 5-DEFAULT
handleError1 6-stack:h1,h2
handleError1 7-stack:h1,h2
The following workaround can be used:
<?php
error_reporting(0);
set_error_handler('handleError1');
trigger_error('1-stack:h1');
set_error_handler('handleError2');
trigger_error('2-stack:h1,h2');
trigger_error('6-stack:h1,h2');
trigger_error('7-stack:h1,h2');
function handleError1($code, $message, $file = '', $line = 0, $context = array())
{
echo __METHOD__ . ' ' . $message . PHP_EOL;
}
function handleError2($code, $message, $file = '', $line = 0, $context = [])
{
restore_error_handler(); // This will restore the previous error handler
set_error_handler('count', 0); // Set a dummy method for error handling, it will never be
called because $error_type = 0
try
{
trigger_error('3-DEFAULT');
echo __METHOD__ . ' ' . $message . PHP_EOL;
set_error_handler('handleError3');
trigger_error('4-stack:h1,h2,h3');
restore_error_handler();
trigger_error('5-DEFAULT');
}
finally
{
restore_error_handler(); // Restore the previous error handler
set_error_handler('handleError2'); // Set the current error handler again
}
}
function handleError3($code, $message, $file = '', $line = 0, $context = [])
{
echo __METHOD__ . ' ' . $message . PHP_EOL;
}
?>
which will output:
handleError1 1-stack:h1
handleError2 2-stack:h1,h2
handleError3 4-stack:h1,h2,h3
handleError2 6-stack:h1,h2
handleError3 4-stack:h1,h2,h3
handleError2 7-stack:h1,h2
handleError3 4-stack:h1,h2,h3
----
Server IP: 72.52.91.14
Probable Submitter: 80.77.114.150
----
Manual Page -- http://php.net/manual/en/function.restore-error-handler.php
Edit -- https://master.php.net/note/edit/120879
Del: integrated -- https://master.php.net/note/delete/120879/integrated
Del: useless -- https://master.php.net/note/delete/120879/useless
Del: bad code -- https://master.php.net/note/delete/120879/bad+code
Del: spam -- https://master.php.net/note/delete/120879/spam
Del: non-english -- https://master.php.net/note/delete/120879/non-english
Del: in docs -- https://master.php.net/note/delete/120879/in+docs
Del: other reasons-- https://master.php.net/note/delete/120879
Reject -- https://master.php.net/note/reject/120879
Search -- https://master.php.net/manage/user-notes.php