This is a discussion on excessive use of include files within the PHP Language forums, part of the PHP Programming Forums category; Is there anything wrong with using a lot of include files? On one part of my website, I have a ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Is there anything wrong with using a lot of include files?
On one part of my website, I have a form. The page it posts data to, includes a different file based on some of the values. The include files also include other pages based on different values. All of these work together to select the right information from the database and display it in the correct format. I'm just wondering if it is better to put all the code in one file rather than in the include files. Thanks, -Tom |
|
|||
|
On 16 Sep 2004 11:31:45 -0700, stry_cat@yahoo.com (Tom Cat) wrote:
>Is there anything wrong with using a lot of include files? > >On one part of my website, I have a form. The page it posts data to, >includes a different file based on some of the values. The include >files also include other pages based on different values. All of >these work together to select the right information from the database >and display it in the correct format. > >I'm just wondering if it is better to put all the code in one file >rather than in the include files. There is an argument that pulling the include files together takes time, but if you include library files that contain hundreds of functions, it ends up causing the same kind of issue. In one of my larger projects (a content management system) I ended up programatically deciding which includes to process, based on what was required for the page... you have to design that kind of system into your code from the beginning though. Just my two penneth :) Jonathan Beckett (jonbeckett@pluggedout.com) working on : http://www.pluggedout.com/penpals |
|
|||
|
stry_cat@yahoo.com (Tom Cat) wrote in message
news:<fa408b52.0409161031.69c842ad@posting.google. com>... > > Is there anything wrong with using a lot of include files? Performance-wise, no, if the server is equipped with a PHP accelerator, and yes, if it isn't. Opening a file takes time, but PHP accelerators take care of the problem by compiling scripts and caching the executable code. Style-wise, it depends... If each include file has a good reason to be separate from the rest (for example, if it contains a group of functions/classes implementing a distinct functionality) and has been thoroughly tested prior to including into the application, there's nothing wrong with using a large number of includes. Cheers, NC |
|
|||
|
If you have 200 include files containing one function each then it will take
longer to load that one include file containing all 200 functions. 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 can also have different versions of functions in different files, so I can load include.xml.php4.inc or include.xml.php5.inc depending on which version of PHP I am running. It takes a little up-front organisation, but I think it's worth it. -- Tony Marston http://www.tonymarston.net "Tom Cat" <stry_cat@yahoo.com> wrote in message news:fa408b52.0409161031.69c842ad@posting.google.c om... > Is there anything wrong with using a lot of include files? > > On one part of my website, I have a form. The page it posts data to, > includes a different file based on some of the values. The include > files also include other pages based on different values. All of > these work together to select the right information from the database > and display it in the correct format. > > I'm just wondering if it is better to put all the code in one file > rather than in the include files. > > Thanks, > > -Tom |
|
|||
|
Tom Cat <stry_cat@yahoo.com> wrote or quoted:
> Is there anything wrong with using a lot of include files? Too many include files is bad - but the much more common sin is not splitting things up enough. -- __________ |im |yler http://timtyler.org/ tim@tt1lock.org Remove lock to reply. |
|
|||
|
On Fri, 17 Sep 2004 09:50:56 GMT, Tim Tyler <tim@tt1lock.org> wrote:
>Tom Cat <stry_cat@yahoo.com> wrote or quoted: > >> Is there anything wrong with using a lot of include files? > >Too many include files is bad - but the much more common sin is not >splitting things up enough. I agree. I tend to have one include each for... Session Functions - always the first include, doing things like starting the session, processing cookies and so on... Database Functions - generic things like opening and closing a connection HTML Functions - any functions that return HTML - with the aim being to not have HTML in the scripts that are executed. .... and following on from there, a number of includes to do specific stuff. Jonathan Beckett (jonbeckett@pluggedout.com) working on : http://www.pluggedout.com/penpals |
|
|||
|
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. |
|
|||
|
stry_cat@yahoo.com (Tom Cat) wrote in message news:<fa408b52.0409161031.69c842ad@posting.google. com>...
> Is there anything wrong with using a lot of include files? > > On one part of my website, I have a form. The page it posts data to, > includes a different file based on some of the values. The include > files also include other pages based on different values. All of > these work together to select the right information from the database > and display it in the correct format. > > I'm just wondering if it is better to put all the code in one file > rather than in the include files. > > Thanks, > > -Tom Thanks everyone for the answers. After reading them I think I'm ok. -Tom |
|
|||
|
"Tom Cat" <stry_cat@yahoo.com> wrote in message
news:fa408b52.0409171052.16e3a491@posting.google.c om... > stry_cat@yahoo.com (Tom Cat) wrote in message news:<fa408b52.0409161031.69c842ad@posting.google. com>... > > Is there anything wrong with using a lot of include files? > > > > On one part of my website, I have a form. The page it posts data to, > > includes a different file based on some of the values. The include > > files also include other pages based on different values. All of > > these work together to select the right information from the database > > and display it in the correct format. > > > > I'm just wondering if it is better to put all the code in one file > > rather than in the include files. > > > > Thanks, > > > > -Tom > > Thanks everyone for the answers. After reading them I think I'm ok. > Actually, no. What you're doing is using include files in lieu of functions, which is a rather crummy way to program in PHP. |