odbc_errormsg不会以应有的方式报告来自odbc_execute的错误消息。它只是发出警告而已。因此,我被迫编写了一个通过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好好工作。
发布于 2011-12-14 13:12:37
是的,这是一个奇怪的解决方案,但我认为它符合你的目的。
在玩了一会儿之后,我发现:
function my_error_handler ($errno, $errstr, $errfile = '', $errline = 0, $errcontext = array()) {
// Handle the error here
@trigger_error($errstr);
return TRUE;
}
// Just to make sure PHP is not outputting anything
error_reporting(-1);
ini_set('display_errors',1);
set_error_handler('my_error_handler');
// An E_USR error...
trigger_error('Some error');
var_dump(error_get_last());
// ...and a native one
$key = count();
var_dump(error_get_last());这方面的结果:
array(4) {
["type"]=>
int(1024)
["message"]=>
string(10) "Some error"
["file"]=>
string(69) "C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\test.php"
["line"]=>
int(7)
}
array(4) {
["type"]=>
int(1024)
["message"]=>
string(45) "count() expects at least 1 parameter, 0 given"
["file"]=>
string(69) "C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\test.php"
["line"]=>
int(7)
}从错误处理程序中调用@trigger_error(),而不返回FALSE,将导致error_get_last()返回NULL以外的其他内容,但是由于使用@抑制错误,所以PHP不会输出任何内容。似乎为了避免无限递归,从已注册的错误处理程序函数中调用trigger_error()并不会调用错误处理程序--这对我们这里的好处是很好的。
显然,错误代码已经被修改了,但是如果需要的话,可以将其转换为相关的E_USR_*代码--但我怀疑您真正需要的是字符串值,这个方法将允许您得到这个值。不幸的是,您还丢失了行号和文件信息--尽管您可能会通过在错误处理程序中执行涉及堆栈跟踪的操作,或者至少从传递的参数中将其包含在字符串中来获得此信息。
这是一个可怕的,可怕的,可怕的黑客--但是由于没有官方认可的方法来做这件事,黑客基本上就是你想要的。
发布于 2016-12-14 09:03:30
您可以在忽略错误(使用@-操作符)时更改自定义错误处理程序以返回false。
function custom_error_handler($errno, $errstr, $errfile, $errline){
$ignore = ($errno & error_reporting()) == 0;
if ($ignore) {
return FALSE;
}
echo "[Error happened: $errstr]\n";
return TRUE;
}https://stackoverflow.com/questions/8504253
复制相似问题