odbc_errormsg不报告错误消息odbc_execute就像它应该的那样。它只是发出警告。所以我被迫编写一个 hack 来解析错误消息error_get_last.
我在用着set_error_handler and error_get_last
回报NULL
除非我:
禁用我的错误处理程序,
或者让它返回FALSE
.
我认为这是由于 PHP 的内置错误处理程序负责将错误详细信息存储在某处,以便以后可以检索它们。
有没有办法在我的自定义错误处理程序中模拟这种行为error_get_last()
可以正常使用吗?
请注意,我已经知道几种随时检索错误信息的方法。我的问题是如何制作error_get_last
usable.
Update:我想我最好发布一些代码。
PHP has 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
工作顺利。