PHP Conference Japan 2024

restore_error_handler

(PHP 4 >= 4.0.1, PHP 5, PHP 7, PHP 8)

restore_error_handler恢復先前的錯誤處理函式

說明

restore_error_handler(): true

在使用 set_error_handler() 變更錯誤處理函式後使用,以恢復先前的錯誤處理函式(可以是內建的或使用者定義的函式)。

參數

此函式沒有參數。

回傳值

永遠回傳 true

範例

範例 #1 restore_error_handler() 範例

判斷 unserialize() 是否造成錯誤,然後恢復原來的錯誤處理器。

<?php
function unserialize_handler($errno, $errstr)
{
echo
"無效的序列化值\n";
}

$serialized = 'foo';
set_error_handler('unserialize_handler');
$original = unserialize($serialized);
restore_error_handler();
?>

上述範例會輸出

Invalid serialized value.

參見

新增註釋

使用者貢獻的註釋 4 則註釋

edgarinvillegas at hotmail dot com
16 年前
Isolde 的說法有點錯誤。錯誤處理器會透過 set_error_handler() 堆疊,並透過 restore_error_handler() 彈出。以下提供一個例子

<?php
mysql_connect
("inexistent"); // 產生一個錯誤。實際的錯誤處理器是預設設定的

function foo1() {echo "<br>錯誤 foo1<br>";}
function
foo2() {echo "<br>錯誤 foo2<br>";}
function
foo3() {echo "<br>錯誤 foo3<br>";}

set_error_handler("foo1"); //目前的錯誤處理器:foo1
set_error_handler("foo2"); //目前的錯誤處理器:foo2
set_error_handler("foo3"); //目前的錯誤處理器:foo3

mysql_connect("inexistent");
restore_error_handler(); //現在,目前的錯誤處理器:foo2
mysql_connect("inexistent");
restore_error_handler(); //現在,目前的錯誤處理器:foo1
mysql_connect("inexistent");
restore_error_handler(); //現在目前的錯誤處理器:預設處理器
mysql_connect("inexistent");
restore_error_handler(); //現在目前的錯誤處理器:預設處理器(堆疊無法再彈出更多)
?>
masterada at gmail dot com
7 年前
從錯誤處理器內部呼叫 restore_error_handler 可能會導致非預期的行為

<?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'); // 使用 PHP 內建的錯誤處理器

echo __METHOD__ . ' ' . $message . PHP_EOL;

set_error_handler('handleError3');
trigger_error('4-stack:h1,h2,h3');

restore_error_handler(); // 這會恢復到 handleError1,而不是內建的錯誤處理器
trigger_error('5-DEFAULT');
}

function
handleError3($code, $message, $file = '', $line = 0, $context = array())
{
echo
__METHOD__ . ' ' . $message . PHP_EOL;
}

?>

上述程式碼的輸出結果如下:

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

可以使用以下的解決方法

<?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(); // 這會恢復先前的錯誤處理器
set_error_handler('count', 0); // 設定一個虛擬方法來處理錯誤,它永遠不會被呼叫,因為 $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(); // 恢復先前的錯誤處理器
set_error_handler('handleError2'); // 再次設定目前的錯誤處理器
}
}

function
handleError3($code, $message, $file = '', $line = 0, $context = [])
{
echo
__METHOD__ . ' ' . $message . PHP_EOL;
}
?>

輸出的結果為

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
lsole at maresme dot net
20 年前
如同文件中所述,restore_error_handler() 會恢復到*先前的錯誤處理器*... 即使它是相同的。一個錯誤讓我設定了兩次我的自訂錯誤處理器,後來當我呼叫 restore_error_handler() 來恢復內建處理器時,似乎沒有任何反應... 這讓我困惑了一陣子!
TiMESPLiNTER
9 年前
也適用於恢復巢狀錯誤處理器

<?php

error_reporting
(E_ALL);

echo
'<pre>';

set_error_handler(function($errno, $errstr, $errfile, $errline, array $errcontext) {
echo
'錯誤處理器 1: ' , $errstr , PHP_EOL;
});

trigger_error('錯誤 1');

set_error_handler(function($errno, $errstr, $errfile, $errline, array $errcontext) {
echo
'錯誤處理器 2: ' , $errstr , PHP_EOL;
});

trigger_error('錯誤 2');

restore_error_handler();

trigger_error('錯誤 3');

restore_error_handler();

trigger_error('錯誤 4');

?>
To Top