©
Dieses Dokument verwendet PHP-Handbuch für chinesische Websites Freigeben
(PHP 4 >= 4.0.1, PHP 5, PHP 7)
restore_error_handler — 还原之前的错误处理函数
在使用 set_error_handler() 改变错误处理函数之后,此函数可以 用于还原之前的错误处理程序(可以是内置的或者也可以是用户所定义的函数)。
该函数总是返回 TRUE
。
Example #1 restore_error_handler() 范例
如果是 unserialize() 导致了一个错误,接下来 会恢复原来的错误处理函数。
<?php
function unserialize_handler ( $errno , $errstr )
{
echo "Invalid serialized value.\n" ;
}
$serialized = 'foo' ;
set_error_handler ( 'unserialize_handler' );
$original = unserialize ( $serialized );
restore_error_handler ();
?>
以上例程会输出:
Invalid serialized value.
Note:
如果在 error_handler 函数中调用 restore_error_handler() 则会被忽略。
[#1] TiMESPLiNTER [2015-04-27 10:39:28]
Works also for restoring nested error handlers:
<?php
error_reporting(E_ALL);
echo '<pre>';
set_error_handler(function($errno, $errstr, $errfile, $errline, array $errcontext) {
echo 'ErrorHandler 1: ' , $errstr , PHP_EOL;
});
trigger_error('Error 1');
set_error_handler(function($errno, $errstr, $errfile, $errline, array $errcontext) {
echo 'ErrorHandler 2: ' , $errstr , PHP_EOL;
});
trigger_error('Error 2');
restore_error_handler();
trigger_error('Error 3');
restore_error_handler();
trigger_error('Error 4');
?>
[#2] edgarinvillegas at hotmail dot com [2008-03-28 08:21:15]
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:
<?php
mysql_connect("inexistent"); //Generate an error. The actual error handler is set by default
function foo1() {echo "<br>Error foo1<br>";}
function foo2() {echo "<br>Error foo2<br>";}
function foo3() {echo "<br>Error foo3<br>";}
set_error_handler("foo1"); //current error handler: foo1
set_error_handler("foo2"); //current error handler: foo2
set_error_handler("foo3"); //current error handler: foo3
mysql_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)
?>
[#3] lsole at maresme dot net [2004-03-14 11:57:19]
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!