[thelist] PHP script design tips (long)

Garrett Coakley garrett at polytechnic.co.uk
Sat Sep 1 09:24:47 CDT 2001


On Fri, 31 Aug 2001 18:17:16 -0700, "rick" <rick at techno-weenie.com>
wrote:

(this ended up a lot longer than I meant... sorry!)

> I've been playing with PHP for almost a year now, but I have no formal
> programming training.  

Pah... proper training is for wimps, I just let the force be my guide.

> So, I'm getting to the point where I understand
> how most of the PHP functions work and how to accomplish most common
> tasks.  But I find that I'm having problem with fundamental 
> programming issues. 

Yeah.. I have those all the time *:)

One thing I've found that helps is having a trawl through the code of
other PHP projects that are out there (man I love open source). Look at
how they structure their code, how they modularize, keep order, stop
stuff getting broken.

Especially in large projects where there's more than one person hacking
on the code, having a logical structure is a must.

A couple that are worth having a look at:

http://www.theexchangeproject.org
http://www.geeklog.org (hi john!)

> I have a lot of little script design issues like this I'd like some
> advice on, and I'm sure some of you have a lot more experience +
> knowledge. I'd appreciate any input...

Well, not sure whether this is what you're looking for, but this is how
I generally structure my PHP development.

When I'm scoping out the project and getting some ideas on paper things
I think about like...

What does the system need to know? Where does it get it's info from (DB,
flat files, external feed etc), what variables will it expect? This
gives me an idea of what I'm going to to define up front.

How will it achieve it's purpose? What does it need to do? This gives me
a rough outline of what functions I'll need. Things like form
processing, content retrieval, content updates etc.

As for the actual structure, out side of the document root I'll have a
file that sets up all the variables I'm going to need. That'll have
stuff like:

define('HTTP_SERVER', 'http://polytechnic.co.uk');
define('DOCUMENT_ROOT','/var/www/polytechnic');
define('ARE_WE_COOL_OR_WHAT', 1);

so in the logic of the pages I can do lots of conditionalizing and
including without having to define stuff each time. I'll also put all
database functions in one file, basically abstract out which database
I'm using, so I can move the whole thing easily.

This just means that instead of having mysql_fetch_array() all over the
place, I'll redefine to :

function db_fetch_array($qhandle) {
	return @mysql_fetch_array($qhandle);
}

I only have to change one file then, for instance, if I moved to
Postgresql.

Then inside the document root, I'll put a directory with all my included
functions. I might have inc/shopping_cart.php that contains all the
shopping cart logic, inc/forms.php that has the form generation stuff.

I wouldn't be too worried about having lots of includes, this just means
that you know exactly where you're starting from, and the code in each
page can concentrate on doing just whats it's supposed to do.

Plus, when you start a new project, just copy your includes across and
edit them, voila, lovely sexy new base to start from *:)

This rambled a little bit.. sorry about that, hope it helped though.

G "not even gonna touch the professional philosophy thread.. we'd be
here all week!".

-- 
----------------------------------------------------------------------------
WORK: http://spiked.co.uk/
PLAY: http://polytechnic.co.uk/




More information about the thelist mailing list