A Simple and Elegant PHP/MySQL Web Application Framework, Part 1: The Problem

I’ve been developing PHP/MySQL applications for years, both professionally and as a hobby, and if there’s one thing I’ve learned, it’s that there are an infinite number of wrong ways to do it and very few right ways. The vast majority of open source PHP/MySQL web apps are horrendous pieces of garbage. Even the ones that look pretty and clean on the outside are, more often than not, an utter mess inside. This is because the vast majority of PHP/MySQL developers—even the ones who think they’re the hottest shit that was ever shat—place more importance on the end result than on how they get there. And believe me, the ends rarely justify the means when it comes to programming.

It’s an easy trap to fall into, especially in web development. The thing that makes web development so sexy is the instant gratification; you write some code, you click a button, and you’re looking at the result of the code you just wrote. Since the dawn of time (by which I mean the 1970s), books and teachers have been using the concept of instant gratification to get people interested in programming and to keep them coming back for more. This is why the first program you learn to write in a new language will always be “Hello World”; it gives you a very simple taste of the language, provides instant gratification, and leaves you thinking, “Well, that was easy. I want to learn more!”

This makes people lazy, and this laziness is enhanced when the language is as simple to learn and implement as PHP. Why spend a lot of time coming up with an elegant way to separate logic, content, and presentation when you could just toss everything together, copy some example code off a few websites, say a prayer, and get an instant result?

I was certainly guilty of this sort of thinking once upon a time. PHP makes it ridiculously easy to write messy, unsecure, unmaintainable code that nevertheless produces beautiful results. I should know; I’ve written my fair share of crappy PHP code. But with a little thought and some attention to detail, PHP also makes it easy to write beautiful, elegant, secure, maintainable code that produces beautiful results.

After years of writing framework after framework for my web applications and never being fully satisfied, I’ve finally hit on one that works well and makes me happy. It evolved over a period of about two years, gradually getting simpler, more elegant, and more maintainable, until it finally reached a stage where I was able to use the same concept in several web applications of various sizes without altering it. It does away with many of the most common PHP annoyances, makes security painless, and is simple enough and flexible enough that maintaining old features and adding new ones is a breeze.

Over the next few days, I’ll describe the concepts behind the framework, how they evolved, and how I’ve chosen to implement them. There will be lots of example code and lots of me making fun of old code I’ve written. So if this sort of thing interests you at all, stay tuned. Otherwise, be patient. It’ll all be over soon, I promise.