discussion: php script design

mindstorm8191

New Member
Messages
24
Reaction score
1
Points
3
Personally, I'm not a very skilled web designer, but I have been coding or quite a while now, in various languages other than for web design. My current project is a browser-based game, which as I continue developing it, there becomes increasingly more tasks that must be performed, and these new tasks seem to be dropped into the same files I have already been using. Right now I have a script that is over 600 lines long, and compared to what I'm use to, this is crazy.

I would like to open a discussion about how people lay out their written code. Do you like to use as few files as possible? Or do you like to divide everything into short, simple scripts that each do simple tasks? I am curious to know if anyone feels that such a large file is a bad thing or not.
 

diabolo

Community Advocate
Community Support
Messages
1,682
Reaction score
32
Points
48
well as for all the scripts I have written so far, I have not gone back to optimize any of them.

because when I write the script I just focus on getting it done, usable, and semi-easy to use
so for me, I'll just write a script, it it is short, its short. But if I need more lines, i'll just have to write more.
 

Scoochi2

New Member
Messages
185
Reaction score
0
Points
0
When a PHP file is executed, the whole file must be read and checked for errors. So obviously, if you keep running a large file, more and more checking needs to be done.
If you organise the site into many smaller files and only run when you need to perform that specific task, much less checking will need to be done.
In addition, this will make it easier to organise/update your game and resolve problems when they occur.
 

mindstorm8191

New Member
Messages
24
Reaction score
1
Points
3
Hmm, I honestly don't know how much dividing my files would be. The largest file I have is an event handler, which runs any necessary code after a given time point has passed. I could divide this file so that it only calls the needed file for each task, but it may end up having to load most of the tasks (at least what's currently available). Would this be any faster than having it all in one location?
 

garrettroyce

Community Support
Community Support
Messages
5,611
Reaction score
249
Points
63
The other thing to consider is that once your page is run once, PHP compiles it into bytecode so subsequent loads are much quicker. So, most of these things are less of an issue then.
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
I don't find file size (in terms of lines or bytes) or file count to be useful metrics, since they're rather simplistic and don't address design concerns. My main development guideline is "reduce dependencies"; each line should depend on other lines, each function on other functions, each class on other classes as little as possible. My second guideline is each file should be a logical unit, such as a class or a group of inter-related functions (what would be termed a "module" in languages that support the concept). A directory might also be used to group files into a module, especially to group classes together. Occasionally, I might put more than one class in a file if all but one are internal helper classes (what would be an inner class in C++). I tend to have many small functions/methods (a few lines each) rather than a few monolithic functions, but again it's more a matter logical tasks than line counts.

The public API for the classes and modules heavily determines what goes in a file. The class/module should have a single, well defined purpose. Ideally, you should be able to describe the class in one sentence and its API should be somewhat self-documenting. Watch Joshua Bloch's How to Design a Good API and Why it Matters (or read the notes) for more on this.
 
Last edited:
Top