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.