You are here: Re: Question about including files and server load « PHP « IT news, forums, messages
Re: Question about including files and server load

Posted by Oliver Grδtz on 10/14/05 18:10

Jay Paulson schrieb:

> function xyz($abc) { return include(xyz_func.php); }
> function abc($xyz) { return include(abc_func.php); }

Oh. My. God. Is this ugly. OK, it works, but that's not the way how one
should abuse include().

> I was wondering isn't this putting a bigger load on a server by including
> so many files for each function? Also, I was wondering what everyone's
> opinion was on this approach in terms of maintenance. Do you think it's
> better practice to put all your functions in one file or do it in this
> manner?

First of all: Yes, it increases the load, but not as much as one might
think. Modern filesystems are pretty good at caching so after the first
access to your scripts after some pause (caches cleared) there is a big
overhead but for subsequent page requests the overhead will be almost
negligible. So: Favor well organised file collections over big bloated
single-file function libraries!

But: Don't do it this way! Group the functions into similar ones (e.g
all functions regarding mail into functions/mail.php) and put each group
in one file. Rule of thumb: Starting at over 500 lines you begin to get
more problems at maintaining your script files. Then it makes sense not
only grouping the functions into one file per function group but into
directories containing one to many files (e.g. functions/mail/send.php
and functions/mail/receive.php).

Following this you should of course start to not include your whole
function library in each request but to implement some library loader.
You could do this in your pages:

// your index.php (if using this for all pages) or something
// you include in each file

// this is a page sending html mails so I need this:
$used_libs=array('mail/send','html/create');

foreach ($used_libs as $lib)
{
include('functions/'.$lib.'.php');
}

You could extend this concept to check if the requested files exist and
you could add some array with libraries that you want always to be
included. You could also create a mail/all mechanism that includes all
of the files in one directory.

And with PHP5 you have even more choices. You could use classes instead
of functions. Even if you don't want to instantiate objects you could
use them as "namespace simulators" in a static way. Instead of

function mail_send() {}

you could write

class Mail
{
public static function send() {}
}

This would then be called via Mail::send(). What's the advantage? Well,
you can cretae an autoloader for classes with PHP5. If you write it so
that the class Mail is in the file classes/Mail.php then you can
autoload the class on access so you can

Mail::send()

without having to explicitly include anything! Youd wouldn't have to
configure your scripts since only those classes you use will be
included. This of course reduces your server load.

So, long enough a response ? ;-)


AllOLLi

____________
6: "Dr. Amrak gave the disc to me before he died."
B: "What? As opposed to after he died?"
[Battlestar Galactica 107]

 

Navigation:

[Reply to this message]


УдалСнная Ρ€Π°Π±ΠΎΡ‚Π° для программистов  •  Как Π·Π°Ρ€Π°Π±ΠΎΡ‚Π°Ρ‚ΡŒ Π½Π° Google AdSense  •  England, UK  •  ΡΡ‚Π°Ρ‚ΡŒΠΈ Π½Π° английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Π‘Π°ΠΉΡ‚ ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ Π² Π‘Ρ‚ΡƒΠ΄ΠΈΠΈ Π’Π°Π»Π΅Π½Ρ‚ΠΈΠ½Π° ΠŸΠ΅Ρ‚Ρ€ΡƒΡ‡Π΅ΠΊΠ°
ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ΠΈΠ΅ ΠΈ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠ° Π²Π΅Π±-сайтов, Ρ€Π°Π·Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ½ΠΎΠ³ΠΎ обСспСчСния, поисковая оптимизация