Requiring code in PHP

While writing the code to handle a small form in PHP, I just realized that I have a very bad habit — and many just do the same.

When I write a new file, I place all the includes at the very top, before anything else happens. But in my current script, there are many code paths that do not require the major part of all those includes. Only in one specific instance do we require the bulk of the code. Previously, any invocation of that script would have gotten all the code dragged in. Now, I’ve moved the include to just where I need the code (basically, going into a specific case of a larger switch statement … And the load on the web server has just been reduced, without any change to the functionality.

So why do we all put the includes on top?

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.