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 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?