What's the point of having both include and require constructs in PHP?
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 sort开发者_开发问答s 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?
I'd use require
for loading files essential for the app itself, i.e. require 'database_config.php'
, require 'core.php'
. If this fails, you want it to fail as fast, hard and merciless as possible, since something is obviously wrong with your app installation. In this state it's not even guaranteed that it could handle a thrown exception properly, and require
produces a very clear error message without any extra code.
include
should be used for things like template files that you want to use when the app is already up and running and can handle its own errors gracefully.
Example:
include 'error_handler.php';
set_error_handler('error_handler');
/* something bad happens */
Warning: 'error_handler.php' not found!
Error: Specified error handler "error_handler" doesn't exist.
At some point you simply rely on basic files, even if it's just your error handler. You'd have to introduce extra code to handle a missing error handler gracefully, and even then the best you could do is output some error and die
(unless you want to get into the game of catching errors gracefully even if your error handler doesn't exist). It's best to simply require 'error_handler.php'
; you can include
and custom handle everything after this if you want to.
What sorts of files would you really want to optionally include in an application?
I normally use include
with static HTML blocks (header, footer, menu, search box, survey form...) and require
with PHP functions or classes. It's unlikely that the footer block will disappear but, well, I suppose it's a nice visual clue when I look at the code.
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?
The @ operator is said to be expensive and, anyway, I think that including an external file is a basic operation that should be covered by basic language. Your code also adds a new dependency: if you store it in a required block your optional blocks will no longer be 100% optional ;-)
精彩评论