Requiring code in PHP

While wri­ting the code to hand­le a small form in PHP, I just rea­li­zed that I have a very bad habit — and many just do the same.

When I wri­te a new file, I place all the includes at the very top, befo­re any­thing else hap­pens. But in my cur­rent script, the­re are many code paths that do not requi­re the major part of all tho­se inclu­des. Only in one spe­ci­fic ins­tan­ce do we requi­re the bulk of the code. Pre­vious­ly, any invo­ca­ti­on of that script would have got­ten all the code drag­ged in. Now, I’ve moved the include to just whe­re I need the code (basi­cal­ly, going into a spe­ci­fic case of a lar­ger switch state­ment … And the load on the web ser­ver has just been redu­ced, without any chan­ge to the func­tio­na­li­ty.

So why do we all put the inclu­des on top?

Schreibe einen Kommentar

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