Why include_once and require_once may make you a crappy coder

Over the last few years, I’ve noticed that the PHP community has, in general, started to favor include_once() and require_once() over the more standard include() and require(). For the uninitiated, the “once” version of each function will check to see if a file has already been loaded. If it has, it will safely bypass loading the file again without throwing an error, and continue parsing you code. For the really uninitiated, here’s the difference between require() and include() straight from the manual:

require() and include() are identical in every way except how they handle failure. They both produce a Warning, but require() results in a Fatal Error. In other words, don’t hesitate to use require() if you want a missing file to halt processing of the page. include() does not behave this way, the script will continue regardless. Be sure to have an appropriate include_path setting as well.
At first glance, this looks great! Since most files are only ever parsed once—did you know that it is legal to load the same file repeatedly?—these functions will save you from screwing up your code. No more worrying about reloaded files, repeating actions that cause aberrant behavior or flat out fatal errors. Obviously the dynamic coding newbie uses these functions as training wheels. “I may have loaded, but I’m gonna load it again, just in case.” That’s understandable, and all well-and-good. If this is you, don’t make it a habit, learn to structure your code properly so that files that should be loaded once only ever have one opportunity to do so. At the opposite end of spectrum, there are big-time PHP projects that prefer the _once versions exclusively. My own beloved Zen Cart has slowly been making the switch (new major revisions on the horizon do it more sweepingly). The CakePHP Coding Standards actually demand the use require_once():
When including files with classes or libraries, use only and always the require_once function.
Here the rationale is completely different, and completely informed. Because these projects are designed to allow extensive 3rd part manipulation, the chances for a file collision are fairly high. Think about situations where two different mods require the presence of a third “standard” mod library. They trade off a fair amount of performance to offer this flexibility, but at least they know what they’re doing. If you’ve read this far, chances are you are not a newbie, but you sure as hell aren’t Zen Cart or CakePHP either. So, what business do you have using include
once() or require_once()? None, truth be told. And if you use them extensively, then the title of this article was written for you. Congrats! The problem that these functions introduce for most developers is two-fold. First, there’s a performance penalty for using these functions, because the function must first check to see if a file has been loaded. Standard include() and require() statements don’t perform such checks, they simply look for the file and load it. Any type of dynamic system setup is going to use these functions quite a bit, and the penalty is incurred each time the function is used. It only takes about a dozen or so calls to see a difference. Second and more importantly, it feeds laziness. If loading order and structure don’t cause code to fail, then you’re naturally tempted not to worry about them. This leads to unnecessary calls—“Did I load file yet? Ah screw it, I’ll do it again to make sure…”—which feels eerily similar to the rationale of the newbie coder I described above. I’ve learned that code naturally snowballs in one direction or the other. If you write good clean code, optimize where possible (without going overboard), and completely smash bugs, the code you write and your ability to code will only improve. If instead you opt to ignore formatting, write just until “it works” and move on, and/or create logic that fixes a bug symptom rather than the bug itself, you will not improve and your code will suck. That’s really the great tragedy; you’ve bypassed an opportunity to potentially improve your code, to potentially improve your own ability. Any shlub can half-ass it; take the high road and do the work. That will put you ahead of said shlubs when it comes time to look for a new job or get a promotion. I’m not saying any of that is true for you (is it?), but include_once and require_once definitely fall squarely in the lazy coder category as far as I’m concerned.