Click to See Complete Forum and Search --> : coding style
ober0330
Feb 27th, 2004, 11:23 AM
I'm just wondering what you all think is better. Often, I find myself adding more and more functionality to one page. All the functions and displays for one "area" is done under one file, and depending on the variables sent to that page, I display different reports/displays/etc.
Is it better to centralize things like this, or create a new page for each type of function?
kows
Feb 27th, 2004, 10:21 PM
I usually put all of my globally used functions into one central file that will be included into all others being used, usually my 'config' file, which also holds all of my MySQL connection variables and other things like that. If I have a function that is only used in one file, I only have it in that file.. just my habit of doing things, since I usually find that when I'm coding and I decide to make a function I usually just make it in whatever file I'm working on, because I don't often re-use most of my functions outside of one file..
Not sure if that's what you wanted..
CornedBee
Feb 28th, 2004, 01:13 PM
Don't do that much practical PHP, but when I want everything handled by one file (index.php) I use includes. Just include the content php you need for your particular situation in the main index at the right position.
ober0330
Mar 1st, 2004, 06:58 AM
Well, I guess my situation is more like this: I have several different reports that I create, and instead of making a different page for each report and calling an include, I put all the code into one page and use a switch statement to get to the part I want.
I think it's a good way to go about it, but I guess the drawback is a 400-500 code line document. Not that it matters much because none of this is outside our internal net, so it's not like a modem user is going to parse through it.
CornedBee
Mar 1st, 2004, 08:51 AM
How about applying this conversion to your files?
Current:
// index.php
switch($repnum) {
case 1:
// Code for report #1
break;
case 2:
// Code for report #2
break;
default:
// Error message
break;
}
New:
index.php
switch($repnum) {
case 1:
include("reports/rep01.php");
break;
case 2:
include("reports/rep02.php");
break;
default:
include("reports/error.php");
// Error message
break;
}
// rep01.php
// Code for report #1
// rep02.php
// Code for report #2
// error.php
// Error message
ober0330
Mar 1st, 2004, 10:04 AM
Well, again, it's all about minimalization... Why make 4 or 5 different files when I can make 1?
kows
Mar 1st, 2004, 01:53 PM
Originally posted by CornedBee
New:
index.php
switch($repnum) {
case 1:
include("reports/rep01.php");
break;
case 2:
include("reports/rep02.php");
break;
default:
include("reports/error.php");
// Error message
break;
}
If you're planning to do that, you could even just do:
index.php
if(file_exists("reports/rep_" . $repnum . ".php")){
include("reports/rep_" . $repnum . ".php");
}else{
//error message
include("reports/error.php");
}
I'd tend to do the way CornedBee suggested or the way I did it, as that's how I've always done it.. Even if it's on the same network/used by few people.. I think it might be the fact that it potentially shows off your geekiness.. much more efficient.
ober0330
Mar 1st, 2004, 02:23 PM
I would challenge the geek factor. Considering I can pack all my code into one page, I think that would prove to be more efficient.
CornedBee
Mar 1st, 2004, 02:56 PM
The advantage of separation is maintainability (god, I hate this word). You can find stuff far easier if you use includes.
kows
Mar 1st, 2004, 04:52 PM
Actually, it wouldn't be more efficient because everytime you execute your script you're loading everything in the page. Otherwise, you're only loading what you need to in the page.
ober0330
Mar 2nd, 2004, 07:27 AM
Ok, I see your point, but I'm still leaving mine as it is.
I may go that route in the future.
The Hobo
Mar 2nd, 2004, 11:44 AM
Originally posted by kows
Actually, it wouldn't be more efficient because everytime you execute your script you're loading everything in the page.
Which means it would use a lot more memory than it needs to.
David.Poundall
Mar 8th, 2004, 06:42 AM
If you have several forms that are interactive, using the include method allows you to work on each individually with a different file name in the editor.
This saves you scrolling up and down or using single file bookmarks which can be a pain. Name each file so that it has a reference to the switch condition in it and it makes it even more intuitive.
The top level switch file is very usefull for testing for form to form jumps. As the sub forms all point to the main switch file with a pointer to where they would like to jump to, you can do checks to see if all fields are filled before allowing the form jump.
If there is a field unfilled you can trap this and change the switch pointer to load the original form up highlighting the fields that have been missed.
Also, to make things easier to handle, all of the state control code can be grouped at the bottom and the top of the top level switch form. The sub forms can then concentrate of display issues.
You may also find - as I did - that the top level form then becomes more of a descriptive document that explains the interform logic.
This is home grown but I have used it well with up to five interractive forms. I would be interested to hear if anyone has taken this sort of approach any further.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.