Re: Question about including files and server load

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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]

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux