Grouping Included Functions-How much is too much? [RESOLVED]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
opengavel
Forum Newbie
Posts: 10
Joined: Tue Sep 12, 2006 2:06 pm
Location: Chicago

Grouping Included Functions-How much is too much? [RESOLVED]

Post by opengavel »

I was just wondering if anyone has any guidelines or rules-of-thumb on how many included functions is too many?

I have about 10 or 12 functions that I use across say 30 pages. Is it better to group them all in one include file, break them up into say 3 or 4 different include files (if possible), or nest them (grouping them by what they do -- i.e., file manip functions, DB functions, etc.) I know this doesn't seem like a lot of includes, but I expect the number to grow substantially and I would like to settle on a general approach before it grows much more.

It would seem that nesting them would be the most orderly way, but would that affect the speed or performance of the scripts?

In other words, is the script slowed down by the functions it does not use or by too many file inclusions?

Thanks,

(P.S., Although this isn't a code question, I wasn't sure it should go in Theory and Design either since the question is not "advanced").
Last edited by opengavel on Thu Sep 21, 2006 11:01 am, edited 1 time in total.
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

I would group them by what they do, and include them all. This way the code would be most readable.

I think

Code: Select all

include('functions/db.php');
include('functions/manip.php');
is better than

Code: Select all

include('functions/one.php');
include('functions/two.php');
include('functions/three.php');
include('functions/four.php');
or

Code: Select all

include('functions.php');
User avatar
jwalsh
Forum Contributor
Posts: 202
Joined: Sat Jan 03, 2004 4:55 pm
Location: Cleveland, OH

Post by jwalsh »

Files are cheap. Organization is key to portable code.
opengavel
Forum Newbie
Posts: 10
Joined: Tue Sep 12, 2006 2:06 pm
Location: Chicago

Post by opengavel »

Thanks for the replies.

I am still wondering a couple of things:

1. Is a PHP script slowed down by unused functions?

2. Even if I group them by topic and include them, do others generally include all the functions or just the groups that include functions you know you will use? I assume the answer to this depends on the first question.

3. Lastly, does anyone have a simple script that can output a script's execution time, so I can measure the speed difference, if any?

Thanks again
User avatar
jwalsh
Forum Contributor
Posts: 202
Joined: Sat Jan 03, 2004 4:55 pm
Location: Cleveland, OH

Post by jwalsh »

Unless you are including massive (and I mean MASSIVE) amounts of unused functions, you won't notice a difference.

Calculating execution time is easy

Top of script:

Code: Select all

$begin = microtime();
End of Script:

Code: Select all

$end = microtime();
$elapsed = $end - $begin;
echo "Execution Time: " . $elapsed . " milliseconds";
opengavel
Forum Newbie
Posts: 10
Joined: Tue Sep 12, 2006 2:06 pm
Location: Chicago

Post by opengavel »

Thanks
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

PHP in terms of execution wouldn't be slowed by unused functions, but parsing time is increased and therefore does effect your overall PHP performance. Reduce the amount of unused functions as much as possible.

Modularity is key to PHP development success.

Files under ext2 FS are actually accessed faster when they fall under the 12*1024 bytes rule. Meaning large files are accessed slower than small files (1024*12 = 12228 bytes OR LESS) so modularity, although requiring more includes and thus more wasted disk space *can* actually be faster in terms of overall execution.
Post Reply