error_get_last()和自定义错误处理程序

Álv*_*lez 7 php error-handling

odbc_errormsg 不会按照odbc_execute预期的方式报告错误消息.它只是抛出一个警告.所以我被迫写了一个hack来解析错误信息error_get_last.

我正在使用set_error_handlererror_get_last返回,NULL除非我要么:

  1. 禁用我的错误处理程序

  2. 或让它回来FALSE.

我想这是由于PHP的内置错误处理程序负责将错误详细信息存储在某处,以便以后可以检索它们.

有没有办法在我的自定义错误处理程序中模拟这种行为,所以error_get_last()可以正常使用?

请注意我已经知道几种方法可以随时检索错误信息.我的问题是如何error_get_last使用.


更新:我想我最好发布一些代码.

PHP有error_get_last(),允许这样做:

@fopen('xxx');
var_dump( error_get_last() );
Run Code Online (Sandbox Code Playgroud)

......得到这个:

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)
}
Run Code Online (Sandbox Code Playgroud)

如果您替换内置错误处理程序,这会中断:

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
Run Code Online (Sandbox Code Playgroud)

如果你保留两个错误处理程序......

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;
Run Code Online (Sandbox Code Playgroud)

......你得到副作用:

[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
Run Code Online (Sandbox Code Playgroud)

......而不仅仅是:

[Error happened: Undefined variable: foo]
Run Code Online (Sandbox Code Playgroud)

我想我的自定义错误处理程序正确接口error_get_last.我想error_get_last工作得很好.

Dav*_*dom 6

是的,这是一个奇怪的解决方案,但我认为它将适合您的目的.

经过一段时间的游戏,我发现了这个:

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());
Run Code Online (Sandbox Code Playgroud)

结果如下:

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)
}
Run Code Online (Sandbox Code Playgroud)

@trigger_error()从错误处理程序中调用,而不是返回FALSE,导致error_get_last()返回其他内容NULL,但由于错误被抑制@,PHP不输出任何内容.似乎为了避免无限递归,trigger_error()在注册的错误处理函数内调用不会调用错误处理程序 - 这对我们有利.

显然,错误代码已被修改,但E_USR_*如果需要,您可以将其转换为相关代码 - 但我怀疑您真正想要的是字符串值,此方法将允许您获取.遗憾的是,您还丢失了行号和文件信息 - 尽管您可以通过在错误处理程序中执行涉及堆栈跟踪的操作来获取此信息,或者至少将其包含在传递的参数的字符串中.

这是一个可怕的,可怕的,可怕的黑客攻击 - 但由于没有正式批准的方法,黑客本质上就是你所要求的.