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 includes. Only in one spe­ci­fic ins­tance 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, wit­hout any chan­ge to the functionality. 

So why do we all put the includes on top?


Beitrag veröffentlicht

in

von

Schlagwörter:

Kommentare

Schreibe einen Kommentar

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