douwu7563 2013-11-26 17:58
浏览 77
已采纳

为什么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 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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况