douwu7563
2013-11-26 17:58 阅读 63
已采纳

为什么E_WARNING表现得像E_ERROR?

I am running a script that requires a file which is not properly included in my script.

PHP Fatal error: require_once(): Failed opening required 'Math/BigInteger.php' (include_path='.:/usr/share/php:/usr/share/pear') in /myDir/Net/SSH2.php on line 746

I have an error_handler set up, which records every error I get:

function script_error_handler($errno, $errstr, $errfile, $errline){
    echo "IN ERROR HANDLER
";

    $GLOBALS['errors'][] = array(
        'errno'     => $errno,
        'errstr'    => $errstr,
        'errfile'   => $errfile,
        'errline'   => $errline
    );

    return true;
}

I also have a shutdown_function which later goes through the errors to determine success or failure (among other things). Part of that function prints the errors I have recorded.

function shutdown_handler($io) {
    print_r($GLOBALS['errors']);
    echo "
";
    //irrelevant stuff ommitted
}

Oddly, the output of this is as follows:

Array
(
    [0] => Array
        (
            [errno] => 2
            [errstr] => require_once(Math/BigInteger.php): failed to open stream: No such file or directory
            [errfile] => /myDir/Net/SSH2.php
            [errline] => 746
        )

)

According to PHP's Predefined Constants, 2 is the value of an E_WARNING.

2 | E_WARNING (integer) | Run-time warnings (non-fatal errors). Execution of the script is not halted.

This seems to be in clear conflict with the Fatal Error output I get earlier. Why don't I get an E_ERROR in this situation? What is going on here?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    dsf323233323332 dsf323233323332 2013-11-26 18:08

    require/require_once generate a WARNING and a FATAL ERROR.

    Without your own error handling, you should get something like this:

    Warning: require_once(foo) [function.require-once]: failed to open stream: No such file or directory in […]

    Fatal error: require_once() [function.require]: Failed opening required 'foo' (include_path='.') […]

    Looking at the errstr of your output you see that you get the “failed to open stream: No such file or directory” part – so the text of the WARNING.

    And the description of set_error_handler tells you,

     “The following error types cannot be handled with a user defined function: E_ERROR, […]”

    So the WARNING is caught as was to be expected – and the FATAL ERROR, in accordance with what the manual says, is not.

    点赞 9 评论 复制链接分享

相关推荐