0

I've read about how require and include differ In short, one gives a warning an other quits process execution.

I'd like to simulate the behaviour to see the difference live in a non production environment. I've created two classes in separate files:

namespace includeTest;
class IncludeError{
    public static function errorTrigger(){
        throw new \Exception();
    }
}

and

namespace requireTest;
class RequireError{
    public static function errorTrigger(){
        throw new \Exception();
    }
}

When I test the IncludeError::errorTrigger() with this code

namespace index;
include "includeTest.php";
\includeTest\IncludeError::errorTrigger();
print "done in index.php";

I am getting the usual Fatal error: Uncaught Exception and the "done in index.php" line never prints, indicating that even though the error (actually Exception) happened in "includeTest.php", it was terminating the caller script as well. Clearly exceptions are not the type of errors that give warnings when included. What kind of error would trigger just a warning?

I am using php 7.3.8.

An excerpt from the book I am reading:

The only difference between the include() and require() statements lies in their handling of errors. A file invoked using require() will bring down your entire process when you meet an error. The same error encountered via a call to include() will merely generate a warning and end execution of the included file, leaving the calling code to continue. This makes require() and require_once() the safe choice for including library files, and include() and include_ once() useful for operations like templating.

sanjihan
  • 5,592
  • 11
  • 54
  • 119
  • 1
    Your include successfuly included the requested file. So it gave neither a warning nor an error. So to get a warning include something that is not available. – Adam P. Dec 01 '19 at 13:35
  • Thanks. My understanding is that include and require differ also when an error happens in the included/required script and not only if you include an incorrect file. I attached a snippet from the book I am reading that gave me this impression. – sanjihan Dec 01 '19 at 13:42
  • Yep, sounds wrong to me in the excerpt. – Adam P. Dec 05 '19 at 14:46

0 Answers0