3

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\n";

    $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 "\n";
    //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?

neubert
  • 15,947
  • 24
  • 120
  • 212
MirroredFate
  • 12,396
  • 14
  • 68
  • 100
  • Can you show how you are registering the handlers? – Rob Baillie Nov 26 '13 at 18:04
  • I could, but I don't see how that is relevant. As I get output from both handlers, they are clearly being registered and called. – MirroredFate Nov 26 '13 at 18:05
  • As it appears you have an answer below you probably don't need to, so no bother. However, it makes it easier for the people going out of their way to help you if you post a complete test case and then answer their questions when posed. – Rob Baillie Nov 26 '13 at 18:11

1 Answers1

5

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.

Community
  • 1
  • 1
CBroe
  • 91,630
  • 14
  • 92
  • 150
  • So I should be able to check `error_get_last()`, and if it is an E_ERROR add it to my error list in the `shutdown_function()`. Is that a fair assumption? – MirroredFate Nov 26 '13 at 18:11