Jump to content

PHP include everything or just what is needed?


johnsmith153

Recommended Posts

If I had a class/function that did a specific thing (check a value and return true or false) yet I only need the function on 10% of pages, would I just include it all the time anyway?

 

e.g:

include "usualEssentials.php";

include "specificFunction.php";//function only needed 10% of time

 

...would there be any benefit of stating it seperatley ONLY when it is needed, or should I just include it in ever page as PHP won't care (and put function in the usualEssentials.php file)?

 

I suppose my question is: does PHP slow down / doesn't like functions being declared on a page if it isn't going to be used.

 

Is this the same for JavaScript? (I have an Ajax function which isn't always needed on every page)

You would usually either;

  Use the page to include any "Specific" functions it needs, or

  Use a class object (Display Object (Ajax) + Controller Object(PHP)?) to determine whether the function is necessary.

 

PHP is a very fast engine anyway so unless your expecting around 200 page views a second with big code bases then this isn't needed.

 

-cb-

Can if you want but bad practice.

 

If your just making simple little scripts it really does not matter.

If your making production scripts (that you will give/sell) then you need to think about How your script runs.

 

Usually you have some sort of controller (selects which "page" to load depending on the REQUEST variables like GET and POST).

 

In this controller object/function/code you could add a method/function/code that checks what the page needs, or in fact let the page use a function to include the files it needs.

 

I don't really know how to make this any clearer.

 

Good Luck,

-cb-

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.