While looking through some old code I found this trick I once used for restoring the built-in error handler:
<?php
// Unwind the error handler stack until we're back at the built-in error handler.
function unset_error_handler()
{
while (set_error_handler(create_function('$errno,$errstr', 'return false;'))) {
// Unset the error handler we just set.
restore_error_handler();
// Unset the previous error handler.
restore_error_handler();
}
// Restore the built-in error handler.
restore_error_handler();
}
?>
restore_error_handler
(PHP 4 >= 4.0.1, PHP 5)
restore_error_handler — 以前のエラーハンドラ関数を回復する
説明
bool restore_error_handler
( void
)
set_error_handler() を使用してエラーハンドラ関数を 変更した後、元のエラーハンドラ(組込またはユーザ定義関数)に戻すために 使用されます。
返り値
この関数は常に TRUE を返します。
例
例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.
注意
注意: error_handler 関数の中から restore_error_handler() がコールされた場合、 それは無視されます。
参考
- error_reporting() - 出力する PHP エラーの種類を設定する
- set_error_handler() - ユーザ定義のエラーハンドラ関数を設定する
- restore_exception_handler() - 以前の例外ハンドラ関数を回復する
- trigger_error() - ユーザレベルのエラー/警告/通知メッセージを生成する
restore_error_handler
Timo Frenay
31-Aug-2009 11:23
31-Aug-2009 11:23
sebagr gmail com
22-May-2008 03:54
22-May-2008 03:54
I think what Isole wanted to say is that if you push the same error handler twice, then you'll need to call restore_error_handler() twice if you want to get rid of it:
<?php
function custom_error_handler(){
echo 'My error handler';
return false;
}
set_error_handler('custom_error_handler'); // Stack is: <default error handler> | custom_error_handler
set_error_handler('custom_error_handler'); // Stack is: <default error handler> | custom_error_handler | custom_error_handler
trigger_error('error', E_USER_WARNING); // Will print 'My error handler';
restore_error_handler(); // The stack is <default error handler> | custom_error_handler
trigger_error('error', E_USER_WARNING); // Will still print 'My error handler', since the stack is still dirty with our first custom_error_handler.
restore_error_handler(); // The stack is <default error handler>
trigger_error('error', E_USER_WARNING); // Now this will trigger the default error handler.
?>
edgarinvillegas at hotmail dot com
28-Mar-2008 04:21
28-Mar-2008 04:21
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)
?>
lsole at maresme dot net
14-Mar-2004 07:57
14-Mar-2004 07:57
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!
