views:

52

answers:

4

I have about 10 dynamic php pages which use about 30 functions. Each function is needed in more than 1 page, and every page needs a different subset of functions.

I've been pondering these options:

1- all functions in a single include file: every page loads unneeded code

2- each function in its own include file: too many server requests when loading each page

3 - single include file with conditionals only declaring functions needed based on REQUEST_URI: additional processing when loading each page

4 - one include file per php page, with copies of functions needed by that page: difficult to maintain

How does people handle this scenario? Thanks!

+3  A: 

throw related functions into a library include. include libraries as needed.

Further, if you spend another 5 seconds thinking about this, that will be 5 additional seconds you've wasted

(In case you don't get what I'm saying, worrying about include optimization is about the 5billionth thing on your list of things you should ever worry about, until such time as a reported performance problem from end users and subsequent profiling tells you otherwise.)

Zak
Just looking for places to improve page loading time. Will start elsewhere as you suggest. Thanks
Deg
+1  A: 

Option 1 is the simplest, the easiest to maintain, and probably quicker to run than options 2 and 3. Option 4 would be very very slightly faster to run, at the cost of being a maintenance nightmare.

Stick with option 1.

TRiG
If you have only 30 functions, just throw them into one file and have done with it.
TRiG
They are all in one file at the moment. Cosensus seems to be it's not worthwhile to mess with it. THanks
Deg
+1  A: 

You problem shouts OOP.

Organize code by Classes. Load Classes as needed.

More into PHP OOP.

The impact on your server of loading up 30 functions is despicable compared to the impact of yourself having to maintain a map and possible links to all 30 functions.

Frankie
Um, no. If those 30 functions were all logically related, maybe. The poster doesn't say. But if the functions aren't logically related, then DO NOT just bung them into a class. That is NOT OOP.
Frank Shearar
@Frank Shearar just trying to point the poster in the right direction. He does **seam to be trying to re-use code** and **I never said** to put all the functions in one class. *Just organize code into Classes and Load them as needed.*
Frankie
I haven't found broad groups. Will keep it all together for now, but some reading about OPP may be good for later on... Thanks
Deg
A: 

Start by putting them all in one include_once'ed file.

If performance becomes an issue, start by installing a PHP accelerator (to cache the parsed PHP into opcodes, so constant re-parsing is avoided) on the server.

When maintenance becomes an issue, break them up by function. You'll still end up with a catch-all "util.inc" or "misc.inc" file, though...

gavinandresen
Thanks for your help
Deg
The problem with `.inc` files is that your code may be visible to others. Either tell your webserver to interpret files with the `.inc` extension as PHP, or put your `.inc` files in a directory not visible on the web, or put your included code in `.php` files. This is especially the case if your included code contains sensitive information such as database passwords.
TRiG
... or tell you web server to never, ever display .inc files. Putting included code in .php files accessible to the web server is bad practice, because you're not going to test to see what happens if they are accidently executed, and it is possible Bad Things will happen if they are.
gavinandresen