(PHP 4 >= 4.0.1, PHP 5, PHP 7, PHP 8)
set_error_handler — 設置用戶(hù)自定義的錯誤處理函數
設置用戶(hù)的函數 (error_handler
) 來(lái)處理腳本中出現的錯誤。
本函數可以用你自己定義的方式來(lái)處理運行中的錯誤, 例如,在應用程序中嚴重錯誤發(fā)生時(shí),或者在特定條件下觸發(fā)了一個(gè)錯誤(使用 trigger_error()),你需要對數據/文件做清理回收。
重要的是要記住 error_types
里指定的錯誤類(lèi)型都會(huì )繞過(guò) PHP 標準錯誤處理程序,
除非回調函數返回了 false
。
error_reporting() 設置將不會(huì )起到作用而你的錯誤處理函數繼續會(huì )被調用
—— 不過(guò)你仍然可以獲取 error_reporting 的當前值,并做適當處理。
需要特別注意的是帶 @ error-control
operator 前綴的語(yǔ)句發(fā)生錯誤時(shí),這個(gè)值會(huì )是 0。
同時(shí)注意,在需要時(shí)你有責任使用 die()。 如果錯誤處理程序返回了,腳本將會(huì )繼續執行發(fā)生錯誤的后一行。
以下級別的錯誤不能由用戶(hù)定義的函數來(lái)處理,獨立于發(fā)生錯誤的地方:
E_ERROR
、 E_PARSE
、
E_CORE_ERROR
、 E_CORE_WARNING
、
E_COMPILE_ERROR
、
E_COMPILE_WARNING
,和在
調用 set_error_handler() 函數所在文件中產(chǎn)生的大多數
E_STRICT
。
如果錯誤發(fā)生在腳本執行之前(比如文件上傳時(shí)),將不會(huì ) 調用自定義的錯誤處理程序因為它尚未在那時(shí)注冊。
error_handler
以下格式的回調(callback):
可以傳入 null
重置處理程序到默認狀態(tài)。
除了可以傳入函數名,還可以傳入引用對象和對象方法名的數組。
$errno
,$errstr
,$errfile
= ?,$errline
= ?,$errcontext
= ?errno
errno
,包含了錯誤的級別,是一個(gè) integer。
errstr
errstr
,包含了錯誤的信息,是一個(gè) string。
errfile
errfile
,
包含了發(fā)生錯誤的文件名,是一個(gè) string。
errline
errline
,
包含了錯誤發(fā)生的行號,是一個(gè) integer。
errcontext
errcontext
,
是一個(gè)指向錯誤發(fā)生時(shí)活動(dòng)符號表的 array。
也就是說(shuō),errcontext
會(huì )包含錯誤觸發(fā)處作用域內所有變量的數組。
用戶(hù)的錯誤處理程序不應該修改錯誤上下文(context)。
PHP 7.2.0 后此參數被棄用了。 極其不建議依賴(lài)它。
如果函數返回 false
,標準錯誤處理處理程序將會(huì )繼續調用。
error_types
就像error_reporting 的 ini 設置能夠控制錯誤的顯示一樣,
此參數能夠用于屏蔽 error_handler
的觸發(fā)。
如果沒(méi)有該掩碼,
無(wú)論 error_reporting 是如何設置的,
error_handler
都會(huì )在每個(gè)錯誤發(fā)生時(shí)被調用。
如果之前有定義過(guò)錯誤處理程序,則返回該程序名稱(chēng)的 string;如果是內置的錯誤處理程序,則返回 null
。
如果你指定了一個(gè)無(wú)效的回調函數,同樣會(huì )返回 null
。
如果之前的錯誤處理程序是一個(gè)類(lèi)的方法,此函數會(huì )返回一個(gè)帶類(lèi)和方法名的索引數組(indexed array)。
版本 | 說(shuō)明 |
---|---|
7.2.0 |
errcontext 被廢棄。
使用此參數時(shí)會(huì )導致 E_DEPRECATED 提醒。
|
示例 #1 用 set_error_handler() 和 trigger_error() 進(jìn)行錯誤處理
以下示例展示了通過(guò)觸發(fā)錯誤并以用戶(hù)自定義的程序來(lái)進(jìn)行內部異常的處理。
<?php
// error handler function
function myErrorHandler($errno, $errstr, $errfile, $errline)
{
if (!(error_reporting() & $errno)) {
// This error code is not included in error_reporting, so let it fall
// through to the standard PHP error handler
return false;
}
// $errstr may need to be escaped:
$errstr = htmlspecialchars($errstr);
switch ($errno) {
case E_USER_ERROR:
echo "<b>My ERROR</b> [$errno] $errstr<br />\n";
echo " Fatal error on line $errline in file $errfile";
echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n";
echo "Aborting...<br />\n";
exit(1);
case E_USER_WARNING:
echo "<b>My WARNING</b> [$errno] $errstr<br />\n";
break;
case E_USER_NOTICE:
echo "<b>My NOTICE</b> [$errno] $errstr<br />\n";
break;
default:
echo "Unknown error type: [$errno] $errstr<br />\n";
break;
}
/* Don't execute PHP internal error handler */
return true;
}
// function to test the error handling
function scale_by_log($vect, $scale)
{
if (!is_numeric($scale) || $scale <= 0) {
trigger_error("log(x) for x <= 0 is undefined, you used: scale = $scale", E_USER_ERROR);
}
if (!is_array($vect)) {
trigger_error("Incorrect input vector, array of values expected", E_USER_WARNING);
return null;
}
$temp = array();
foreach($vect as $pos => $value) {
if (!is_numeric($value)) {
trigger_error("Value at position $pos is not a number, using 0 (zero)", E_USER_NOTICE);
$value = 0;
}
$temp[$pos] = log($scale) * $value;
}
return $temp;
}
// set to the user defined error handler
$old_error_handler = set_error_handler("myErrorHandler");
// trigger some errors, first define a mixed array with a non-numeric item
echo "vector a\n";
$a = array(2, 3, "foo", 5.5, 43.3, 21.11);
print_r($a);
// now generate second array
echo "----\nvector b - a notice (b = log(PI) * a)\n";
/* Value at position $pos is not a number, using 0 (zero) */
$b = scale_by_log($a, M_PI);
print_r($b);
// this is trouble, we pass a string instead of an array
echo "----\nvector c - a warning\n";
/* Incorrect input vector, array of values expected */
$c = scale_by_log("not array", 2.3);
var_dump($c); // NULL
// this is a critical error, log of zero or negative number is undefined
echo "----\nvector d - fatal error\n";
/* log(x) for x <= 0 is undefined, you used: scale = $scale" */
$d = scale_by_log($a, -2.5);
var_dump($d); // Never reached
?>
以上例程的輸出類(lèi)似于:
vector a Array ( [0] => 2 [1] => 3 [2] => foo [3] => 5.5 [4] => 43.3 [5] => 21.11 ) ---- vector b - a notice (b = log(PI) * a) <b>My NOTICE</b> [1024] Value at position 2 is not a number, using 0 (zero)<br /> Array ( [0] => 2.2894597716988 [1] => 3.4341896575482 [2] => 0 [3] => 6.2960143721717 [4] => 49.566804057279 [5] => 24.165247890281 ) ---- vector c - a warning <b>My WARNING</b> [512] Incorrect input vector, array of values expected<br /> NULL ---- vector d - fatal error <b>My ERROR</b> [256] log(x) for x <= 0 is undefined, you used: scale = -2.5<br /> Fatal error on line 35 in file trigger_error.php, PHP 5.2.1 (FreeBSD)<br /> Aborting...<br />