Tony Marston wrote:
I
personally group similar functions into the same include file, but have
each database table class in its own file. At runtime I only load the
files that I know I will need.
I tend to structure my include files by task the task they are trying to
acheive rather than by what they do (with a few exceptions).
I tend to use include files in two different ways:
1) include files in the same directory as the host script, which are
typically only included by one or two host scripts: the idea here is to
make the operation of the host script apparent while minimizing the amount
of code the developer has to read.
2) library include files: these are usually outside the document root and
don't change very often.
That doesn't really answer the question, so to address it less indirectly
what are the issues with using includes:
1) someone has already mentioned the issue of the overhead of loading lots
of different files although this wouldn't be top of my list of concerns.
2) seperation of code maturity and layering of functionality should
facilitate the development process.
3) never use relative paths to shared include files (unless its '.' see
above) - its a bit of maintainence issue.
4) about the only problem I have with PHP is that you can get in a mess with
global symbols - e.g. suppose you have a library include which does
authentication. This is used by several different scripts. Developer A
decides that it would be a good idea to add a function 'log_ip()' to his
script. Some time later developer B thinks she needs the same function but
decides to put it in the library. Her scripts all work well with the change
but she doesn't know to test developer A's code which is now broken.
This is solved in other languages by using namespaces or offline linking.
5) Pear advises to put each class definition in its own file. I did consider
this rather wasteful until I came across the __autoload function (OK so you
still don't need to keep each class in its own file but it does make things
a lot simpler). Also phpXref gets a bit confused if you don't do this.
6) Pear also recommends where to include files from. I still take issue with
this, but it is important to adopt a strategy and to stick to it so you and
PHP can find your way around your code. I try to ensure that code in any
file only invokes classes/functions declared in the same file or in a file
which it explicitly includes.
7) most of the time when I say include I really mean 'require_once'
HTH
C.