Search code examples
phperror-handlingrequirephp4

PHP require statement - Fails, but throws E_WARNING instead of E_COMPILE_ERROR


I moved a file in our system to a sub folder for organization, which in turn broke a require statement's relative path. This should have resulted in a E_COMPILE_ERROR warning. However, instead of that error, our custom error handler in the application caught the error as a E_WARNING. So, our customer handler logged the message and the resulting PHP Screen came up empty, as if die(); was run. Here is the log created by our local handler:

E_WARNING main(XXXXXX.php): failed to open stream: No such file or directory xxx/xxx.php Line 9 01-26-2012 09:44:27 AM 01-26-2012 10:03:24 AM

It may be important to note that our system is still using poor old PHP 4.

Any idea why require would throw an E_WARNING? This took a bit longer to figure out what the issue was, since we couldn't see any error message.


Edit: The application was definitely doing a require. The custom error handler is simply using PHP's set_error_handler function. So it shouldn't be called for anything other than warnings, notices, and user errors.


Additional Edit:

Maybe the structure has something to do with this? Here's how its working: (-> means includes, => means requires)

main->File1.php=>File2.php

Does it throw a warning because File1.php is included, but not required? This might make sense to me, but would appear to be a glitch? I might need to test this on PHP 5.


RESOLUTION

I've scanned through our application and found this:

error_reporting(E_ERROR | E_WARNING | E_PARSE);

Therefore, E_COMPILE_ERROR will not show up. Furthermore, @DaveRandom is correct about it throwing a warning, then throwing a fatal error.

Changing it to error_reporting(E_ERROR | E_WARNING | E_PARSE | E_COMPILE_ERROR); shows the error message.


Solution

  • Require emits this warning at least once when you call it on a file that does not exist. It then emits an E_COMPILE_ERROR and dies.

    However:

    The following error types cannot be handled with a user defined function: E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING, and most of E_STRICT raised in the file where set_error_handler() is called.

    So your custom error handler will never catch the error I'm afraid.