|
-
Feb 27th, 2004, 12:23 PM
#1
Thread Starter
Frenzied Member
coding style
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?
-
Feb 27th, 2004, 11:21 PM
#2
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..
-
Feb 28th, 2004, 02:13 PM
#3
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Mar 1st, 2004, 07:58 AM
#4
Thread Starter
Frenzied Member
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.
-
Mar 1st, 2004, 09:51 AM
#5
How about applying this conversion to your files?
Current:
Code:
// index.php
switch($repnum) {
case 1:
// Code for report #1
break;
case 2:
// Code for report #2
break;
default:
// Error message
break;
}
New:
Code:
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
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Mar 1st, 2004, 11:04 AM
#6
Thread Starter
Frenzied Member
Well, again, it's all about minimalization... Why make 4 or 5 different files when I can make 1?
-
Mar 1st, 2004, 02:53 PM
#7
Originally posted by CornedBee
New:
Code:
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:
PHP Code:
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.
Last edited by kows; Mar 1st, 2004 at 02:57 PM.
-
Mar 1st, 2004, 03:23 PM
#8
Thread Starter
Frenzied Member
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.
-
Mar 1st, 2004, 03:56 PM
#9
The advantage of separation is maintainability (god, I hate this word). You can find stuff far easier if you use includes.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Mar 1st, 2004, 05:52 PM
#10
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.
-
Mar 2nd, 2004, 08:27 AM
#11
Thread Starter
Frenzied Member
Ok, I see your point, but I'm still leaving mine as it is.
I may go that route in the future.
-
Mar 2nd, 2004, 12:44 PM
#12
Stuck in the 80s
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.
-
Mar 8th, 2004, 07:42 AM
#13
Frenzied Member
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|