I'm writing a PHP application for the first time (other than toys and exercises), and I'm at a loss to understand why PHP includes both an include
and a require
construct.
Before you write an answer explaining the differences between the two, let me first say that I do understand the difference - include
produces a warning and keeps on going, and require
produces a fatal error. My question is: when would you want to include, but not require a file?
Maybe it's a failure of imagination on my part, but there don't seem to be any files in my application that I don't want to scream about if they're not there. Oddly, this doesn't make me want to use require
since it seems impossible to properly handle a failed require
, so instead I use a helper function along the lines of this (warning: air code):
public static function include($filename) {
if (is_readable($filename)) {
if (!@include($filename)) {
throw new FileNotFoundException("File deleted after readable check");
}
} else {
throw new FileNotFoundException("File missing or unreadable");
}
}
I guess what I'm asking is:
- What sorts of files would you really want to optionally include in an application?
- Whether or not you want a file to be required or optional, why wouldn't you just handle that in a function like a modified version of the code I wrote above?