PHP: Hypertext Preprocessor (original) (raw)
restore_error_handler
(PHP 4 >= 4.0.1, PHP 5, PHP 7, PHP 8)
restore_error_handler — Restores the previous error handler function
Description
restore_error_handler(): true
Parameters
This function has no parameters.
Return Values
Always returns [true](reserved.constants.php#constant.true)
.
Examples
Example #1 restore_error_handler() example
Decide if unserialize() caused an error, then restore the original error handler.
<?php function unserialize_handler($errno, $errstr) { echo "Invalid serialized value.\n"; }$serialized = 'foo'; set_error_handler('unserialize_handler'); <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>o</mi><mi>r</mi><mi>i</mi><mi>g</mi><mi>i</mi><mi>n</mi><mi>a</mi><mi>l</mi><mo>=</mo><mi>u</mi><mi>n</mi><mi>s</mi><mi>e</mi><mi>r</mi><mi>i</mi><mi>a</mi><mi>l</mi><mi>i</mi><mi>z</mi><mi>e</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">original = unserialize(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.02778em;">or</span><span class="mord mathnormal">i</span><span class="mord mathnormal" style="margin-right:0.03588em;">g</span><span class="mord mathnormal">ina</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">u</span><span class="mord mathnormal">n</span><span class="mord mathnormal" style="margin-right:0.02778em;">ser</span><span class="mord mathnormal">ia</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">i</span><span class="mord mathnormal">ze</span><span class="mopen">(</span></span></span></span>serialized); restore_error_handler(); ?>
The above example will output:
Invalid serialized value.
See Also
- error_reporting() - Sets which PHP errors are reported
- set_error_handler() - Sets a user-defined error handler function
- get_error_handler() - Gets the user-defined error handler function
- restore_exception_handler() - Restores the previously defined exception handler function
- trigger_error() - Generates a user-level error/warning/notice message
Found A Problem?
edgarinvillegas at hotmail dot com ¶
17 years ago
`Isolde is kind of wrong. The error handlers are stacked with set_error_handler(), and popped with restore_error_handler(). Here i put an example:
Error foo1";} function foo2() {echo "
Error foo2
";} function foo3() {echo "
Error foo3
";}set_error_handler("foo1"); //current error handler: foo1 set_error_handler("foo2"); //current error handler: foo2 set_error_handler("foo3"); //current error handler: foo3mysql_connect("inexistent"); restore_error_handler(); //now, current error handler: foo2 mysql_connect("inexistent"); restore_error_handler(); //now, current error handler: foo1 mysql_connect("inexistent"); restore_error_handler(); //now current error handler: default handler mysql_connect("inexistent"); restore_error_handler(); //now current error handler: default handler (The stack can't pop more) ?>
`
8 years ago
`Calling restore_error_handler from within an error handler might result in unexpected behaviour:
message,message, message,file = '', line=0,line = 0, line=0,context = array()) { echo __METHOD__ . ' ' . $message . PHP_EOL; } function handleError2($code, message,message, message,file = '', line=0,line = 0, line=0,context = array()) { trigger_error('3-DEFAULT'); // This will use the php's default error handlerecho __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,message, message,file = '', line=0,line = 0, 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:
message,message, message,file = '', line=0,line = 0, line=0,context = array()) { echo __METHOD__ . ' ' . $message . PHP_EOL; } function handleError2($code, message,message, message,file = '', line=0,line = 0, 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,message, message,file = '', line=0,line = 0, 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
`
21 years ago
As the docs say, restore_error_handler() revert to the *previous error handler*... even if it is the same. A bug made me set twice my custom error handler and later when I was calling restore_error_handler() to restore the built-in handler nothing seemed to happen... this puzzled me for a while!
10 years ago
`Works also for restoring nested error handlers:
';set_error_handler(function($errno, errstr,errstr, errstr,errfile, errline,arrayerrline, array errline,arrayerrcontext) { echo 'ErrorHandler 1: ' , $errstr , PHP_EOL; });trigger_error('Error 1');set_error_handler(function($errno, errstr,errstr, errstr,errfile, errline,arrayerrline, array errline,arrayerrcontext) { echo 'ErrorHandler 2: ' , $errstr , PHP_EOL; });trigger_error('Error 2');restore_error_handler();trigger_error('Error 3');restore_error_handler();trigger_error('Error 4');?>`