better habits for database-driven web-apps

by Derek Sivers

Related link:

I've finally gotten into some better habits for my database-driven web-apps. I'm trying to get myself out of years of spaghetti-code.

#1 - Make a shared directory where ALL common classes for this project will go
(example: Client.php Customer.php Item.php Invoice.php etc)

#2 - Create a CVS project for it immediately

#3 - Create a /tests/ directory inside of it, and make a PHPUnit2 test file for each class, and a unit test for each method.

#4 - Make ALL parts of the project (the front-end, the back-end management, the shell-scripts, the members login-area) ALL use the exact same shared classes.

#5 - No SQL anywhere outside the shared classes - any interacting with the database HAS to be done through the classes. Many times I'm tempted to just pass a simple one-line SQL command right in my code, but I resist the urge (remembering years of mess this caused me before) - and keep everything centered around the shared classes.

BEFORE I start in with the web-interface, I make many of the methods/functions I know I'll need, and test them at the unit-test level at a terminal/shell. It's always nice to know that your guts are in good order.

Funny thing is... now that that stuff is out of the way, I'm spending seemingly all my time just on the web UI interface! Damn I wish I had a good re-usable framework for all these web-apps. More on that in my next post.

other good habits?


2004-12-07 07:25:01
One habit I got in recently is to spend more time looking at (and thinking through) the details of my user interface before coding. Until recently, I had always accomplished this by throwing a mockup together in Photoshop, or using a crappy WYSIWYG HTML editor to do a slap dash mock up. Perhaps you already have a method you use for this - your sites are always really usable, so I imagine you must put a lot of thought into it.

Anyway, I recently discovered a great tool for this purpose - called Axure. It's a prototyping tool - you use a WYSIWYG editor to create your web interface, annotate it with little notes about how different elements behave, and then click a button and it spits out a "click through" prototype web site. It's a very quick affair, but can really help you think through interface issues you wouldn't have thought of otherwise.

My other big tip is always unit testing ... but you already do that! :)

2004-12-07 17:07:04
Testing PHP
For testing PHP, I highly recommend Apache-Test. You can read more about using Apache-Test to test PHP applications here:

You can also check out the slides from a talk Geoff Young and I gave about:

Lastly, we have a small demo application and tests, so you can see it in action for yourself:

2004-12-10 20:03:29
php framework
there's probably a kajillion frameworks but i know this one is worth looking at:
The original fusebox framework was developed for ColdFusion but they have a PHP variant which I understand is just as popular.