A quick question of code organisation [php]

Associate
Joined
28 Sep 2003
Posts
1,724
Location
Leamington Spa
Hi, been working on my own little project for managing/displaying my game collection. All is going pretty well so far, except at the moment (yes I know you'll cringe) it's all in one big file - displaying the data from the data base, and the entry etc. This was fine when I was getting all the stuff to work, but now it does, and I want to expand the code it's not going to work.

So, I was wondering how I should try an organise it basically :)

I assume I'll want something like

one file for displaying the contents of the database
one for entry of new data

But I just have a feeling there's more to the layout that I need to do some reading on, any input appreciated :)
 
It's impossible to give accurate advice without seeing the project, but generally use separate files for each distinct part of the system.

> A configuration file which contains most everything you need to edit to setup the system
> A functions file containing all the functions used
> Files for each distinct area of the site

You want to minimise duplication, maximise readability and minimise each individual .php script size due to the nature of PHP - no point in compiling a huuuuuge script to use only one small part of it.

The Smarty PHP templating engine might be useful, too - another step after separating into files is to separate the presentation and programming of the site. If you need an example, Simplegal is a nice little project which uses Smarty well :)
 
Yeah, I realised shortly after I posted that adding my code would help..

Please be gentle.. it's ran on my own machine atm (i.e. so I know using of root is very evil, but I'm only running the server as and when I'm working on the code), I'm focusing on just getting it to work, I know there's lots of stuff that needs TLC - but any suggestions of course welcome.

http://pastebin.com/590912
 
It takes a long time for it to finally click, but learn and use OO as much as possible, keeping with the MVC Patterns.

Be warned though, it is a lot to get used to, if you are not already, but once you have, you are likely to think "Why the hell didn't I follow this stuff before?"
 
I'm actually quite in tune with the OO method of doing things... I know this code doesn't reflect that!

I must admit though, in this case, I don't really know how to apply it! With other stuff I've messed with, mostly games stuff, classes and objects seem obvious and easy to work out... here I'm having difficulty! For example, in that block of code... I *know* it can be organised much better, into seperate files/functions - but I can't quite see how in this case :S

Basically, as I'm sure you can see, this started as a mates work, as I'd hit a stumbling block with some basic php stuff. I asked him to code up a little basic version of it (with me looking over his shoulder). I took it home, and bolted bits on without thinking. Very quickly... i.e. few hours :D, I realised that I've got to be organised about it.

how would someone who doesn't suck with php, go about laying out something like my code? Don't worry, I'm not extracting the urine asking for anyone to write any code for me, just generally how they'd go about laying it out (seems to be the only way I learn things for examples >.<)
 
Last edited:
Back
Top Bottom