odbc_errormsg
不按照應該的方式報告odbc_execute
的錯誤消息。它只是發出警告。所以我不得不通過error_get_last
寫一個黑客來解析錯誤信息。error_get_last()和自定義錯誤處理程序
我使用set_error_handler
和error_get_last
回報NULL
,除非我可以:
禁用我的錯誤處理程序,
或使其返回
FALSE
。
我想這是由於PHP的內置錯誤處理程序負責將錯誤細節存儲在某處,以便稍後可以檢索它們。
有沒有辦法在我的自定義錯誤處理程序中模擬這種行爲,所以error_get_last()
可以正常使用?
請注意我已經知道幾種方法隨時檢索錯誤信息。我的問題是如何使error_get_last
可用。
更新:我想我應該張貼一些代碼。
PHP有error_get_last()
,這使得要做到這一點:
@fopen('xxx');
var_dump(error_get_last());
...並得到這樣的:
array(4) {
["type"]=>
int(2)
["message"]=>
string(46) "fopen() expects at least 2 parameters, 1 given"
["file"]=>
string(69) "C:\Documents and Settings\ALVARO.GONZALEZ\Mis documentos\tmp\test.php"
["line"]=>
int(3)
}
如果更換內置的錯誤處理這打破:
function custom_error_handler($errno, $errstr, $errfile, $errline){
$ignore = ($errno & error_reporting()) == 0;
if(!$ignore){
echo "[Error happened: $errstr]\n";
}
return TRUE;
}
set_error_handler('custom_error_handler');
@fopen('xxx');
var_dump(error_get_last()); // NULL
如果您保留兩個錯誤處理程序...
function custom_error_handler($errno, $errstr, $errfile, $errline){
$ignore = ($errno & error_reporting()) == 0;
if(!$ignore){
echo "[Error happened: $errstr]\n";
}
return FALSE;
}
set_error_handler('custom_error_handler');
error_reporting(E_ALL);
echo $foo;
...你得到的副作用:
[Error happened: Undefined variable: foo]
Notice: Undefined variable: foo in C:\Documents and Settings\ALVARO.GONZALEZ\Mis documentos\tmp\test.php on line 15
Call Stack:
0.0004 329720 1. {main}() C:\Documents and Settings\ALVARO.GONZALEZ\Mis documentos\tmp\test.php:0
...而不是隻是:
[Error happened: Undefined variable: foo]
我想我的自定義錯誤處理程序與error_get_last
正確連接。我想error_get_last
工作正常。
您是否接受使用不同的(用戶定義的)功能?因爲如果你願意,你可以將最後一個錯誤存儲在一個全局變量中,並且有一個函數,它只是返回$ GLOBALS ['varname'];' – DaveRandom 2011-12-14 12:17:23
返回'false'是不可接受的?如果是這樣,爲什麼? – Jon 2011-12-14 12:20:18