Make PHP Suck Less With a Framework

We’ve established already that PHP sucks. Now, there’s still lots of reasons you might find yourself developing a web app in PHP: you have a project that needs doing and you know PHP already, or more likely your client or employer demands it. I know, you’d probably rather be developing in Python or Rails or almost anything else, but don’t worry. PHP’s still going to suck, but there’s plenty you can do to make the suicidal tendencies less intense.

I’ve mentioned phpSprockets already — you did read the post and watch the screencast, right? — and that’s an excellent first step. But that won’t get you very far. You’re still stuck in a land of writing a bunch mysql_connect() and include 'header.php' lines at the top of every file, right?

Hell no, this isn’t 1997!

MVC Makes Everything OK Again

Everyone loves MVC frameworks nowadays. Sure, plenty of MVC frameworks existed before Rails, but that was the one — if you were a web developer, and you somehow hadn’t heard of MVC, Rails made you pay attention.

If you’re still not familiar, then you suck. But for your benefit, MVC basically breaks down your gigantic PHP file into three parts: a Model, which handles database connections and calls; a Controller, which handles requests for pages and any business logic not in your Model; and a View, which actually shows a page to a user.

For PHP there are a few MVC frameworks, and we’ll be discussing two: CakePHP and Zend Framework. We won’t go into a lot of detail, just the quick basics. And these aren’t the only two, just the two that I’m familiar with off the top of my head that I know are full of quality. I’ll have links to their websites, and since everyone loves screencasts nowadays, I’ll link straight to those. Watching someone who’s really familiar with a technology really gives you a feel for whether you’ll like it or not.

Without further ado:

CakePHP

Now, I’ll be honest — my CakePHP knowledge is several revisions out of date. Their website has changed, they’ve added some excellent screencasts, and they seem to be doing very well. That said…

If you’re secretly wishing you could develop against Rails but you can’t, CakePHP is for you. It comes with a code generation tool called bake which can generate basic code for you, a scaffolding feature that will get you up and running quickly, and a Rails-inspired API.

I’m not certain if you can override this, but Cake’s code examples and screencasts all show people coding in a PHP4 OO style — no private $mvar, instead var $mvar, and no public function foo(), just function foo(), things like that. Personally, I’m a stickler for PHP5’s OO system, which isn’t perfect but is worlds ahead of PHP4, so this threw me off. In its defense, it is less verbose, and I’m fairly sure there’s no functional difference for the end user, so it’s just a matter of style.

Zend Framework

I’m a little biased towards ZF, not because I think it’s superior, but because it’s what we use at Innova. So if I have more detail here, don’t take this as a slam towards CakePHP.

Zend Framework is a more traditional PHP framework — it requires PHP5 for all of the new Object Orientation features (face it, PHP4’s OO sucked anyway). You can’t expect less from Zend, the guys that make PHP. Zend comes with a slew of library functionality, from Zend_Mail to Zend_Auth and Zend_ACL. If you’ve ever thought, “I wish I had a library for X”, ZF probably has it.

On the downside, Zend doesn’t come with a code-generation utility (as of 1.0.3 — I haven’t checked out the 1.5 preview yet), and this means that tedious tasks are tedious. For example, in Cake (and Rails), you can have the autogeneration tool create you a basic Model, View, and Controller based on input you give it. This may not seem like much, but having the files created, placed in the right place, and named correctly automatically really saves time. And (again, as of 1.0.3) we don’t have a nice console we can use to peek in on things. Rails has that, and it seems CakePHP has a similar feature.

So, Yeah

If you aren’t using a framework then you’re doing yourself a disservice. Check out one of the two I’ve advocated here, or Google around for the handful I didn’t mention. If you’re familiar with one I skipped, please feel free to plug it in the coomments.

Share This

Tags: , , , ,

One Response to “Make PHP Suck Less With a Framework”

  1. Eugene Says:

    Now everyone is talking about the American economy and eclections, nice to read something different. Eugene

Leave a Reply


Close
E-mail It