Back to the blog

RSS
40 subscribers

Recent Posts

Most Popular Posts

  1. Why you should be using a framework
  2. Five easy things that make you a better web developer
  3. Internet Expletive
  4. Where's the Android hype?

About the Blog

Self portrait

I'm a web designer and web application developer in Melbourne, Australia. If you find anything useful, leave me a comment, and if you need web design, development, or accessibility and usability consulting, contact me! Cheers.

Twitter: joshsharp

a bird

Why you should be using a framework

Saturday 13 Oct, 2007 09:14 PM

There's a reason why PHP is growing rapidly as a server-side scripting language — it's very easy to pick up. Many functions are included without needing any sort of namespace importing, and you don't even have to write OO code if you don't want to. Variables are weakly typed and the syntax is fairly familiar.

But PHP's ease of use is also its downfall. Because there are less restrictions on the structure of the code you write, it's much easier to write bad code. But there is a solution: use a framework.

PHP frameworks like CakePHP, CodeIgniter and the Zend Framework (which I wasn't too taken with) provide a solid structure for your code whilst also offering some extra functionality that would be much harder to replicate on its own. It's important to note, too, the frameworks mentioned follow the MVC pattern, which is fairly common and what I'll talk about below — some of these benefits will apply to other patterns as well, but not all.

Of course, if you're not taken with any of the packages above you can also write your own framework, which I've done and will talk about in the next few posts. But for now, let's have a look at the common benefits of a framework.

MVC structured code

Model-View-Container is a widely recognised design pattern that separates out database and business logic from the presentation layer (in this case, the (X)HTML). Separation of presentation and logic aids in maintainability — clean code is easy, understandable code. It means you're no longer searching for that elusive function returning you an empty array — so it comes from the database? It must be in the model. On the flip side, it also means you can update the look and feel of a site without having to worry about wading through PHP script tags. It's like the server-side equivalent of CSS.

Enforcing of good coding standards

Another effect of MVC code is that it makes it so much easier to code as you should. For example, in my framework Rex, the Model is covered by two abstract classes — DataObjects and DataHandlers. DataHandlers instantiate and manipulate DataObjects, which are basically big associative arrays wrapped lovingly in getters and setters. But here's the neat bit — DataHandlers are the only object which can access the database directly. Of course, you could get around this if you wanted to, but by sticking to rules like these, you save yourself months of headache. If your User records are failing to insert, well no contest — it's an issue with UserHandler. Too easy.

Pretty URLS

Most of these frameworks use a bit of mod_rewrite magic to ensure your URLS are always clean and accessible. Apache handles requests to a particular URL, and (hidden to the client) returns this URL to /index.php as a GET string. The index page instantiates your main framework object (in Rex's case, the App object) and farms the query off, and you're done. But all the end-user knows is that they're at /users/browse/page/2 — they don't ever even know you're using PHP, for that matter!

Accessible URLS also help with SEO — linking to your blog post as /blog/view/why_you_should_be_using_a_framework/ is going to boost your search engine ranking for those keywords more than /blog/view.php?id=12 ever could.

Helpful helpers

Remember how every page query starts from index.php? I promise this will make your life so much easier. Everything you need can be included into a few main files — no more forgetting to include formfunctions.php into that statistics report page. You would not believe how good it feels the first time you realise you never have to include anything manually again.

Of course, with all that spare time you now have, you can get to work learning how to use the helpers that are now helpfully included into every page. No more manual validation of forms — just instantiate a new form validator object and tell it what you need. No more passing error messages in GET strings (and I've seen some terrible examples of this) — your form helper can flag errors on your fields when it reloads the page. Welcome to the land of milk and honey, where there are helper objects for all the fiddly things you once did by hand.

Less time coding — you're so efficient you just put yourself out of a job.

It doesn't take a genius to figure out that using a framework saves you stacks of time and effort. It really does enable you to do things you would only have dreamt of, back when every page had its own controller (or however you choose to do it — that's the sin I was guilty of).

As I mentioned, if you stick around I'll take you through how I wrote my own framework so you can write your own too. It's perfect for anyone too anal to bow down to someone else's way of naming objects :)

Have I missed anything? Are there disadvantages to frameworks you want to mention? Let me know in the comments.

Update: some further thoughts on the topic here.

Tags: framework, php

Comments

Steve said on 14 Oct 2007:
So, what framework Do you recommend?
Josh said on 14 Oct 2007:
Well, it does depend what you're looking for. CakePHP does a lot of nifty stuff for you, if you like that sort of thing, whereas frameworks like Zend and CodeIgniter offer a lot of functionality without taking away the power to do things your own way. Which is the sort of thing I like :) I've heard a lot of good things about CodeIgniter actually, give that one a try.

And of course you can always write your own...
Tane Piper said on 23 Oct 2007:
I absolutely agree with you, and this is why I used CakePHP in all my applications that I build from scratch. My current app is a simple pastebin with lots of bells and whistles that CakePHP allows me to provide (you can see it running @ http://pastemonkey.org.

It might seem a bit overkill for a simple app like this, but the App itself is very small, and uses my central CakePHP library, so it allows me to run as many as I like, as well as other applications.
Miksago said on 23 Oct 2007:
Though, are in some cases frameworks overkill, i'm sure it's nice to have all the widgets and that, but why are they so full of features, it'd be nice to have a 'jquery of php' framework.
Josh said on 23 Oct 2007:
I agree Miksago, sometimes they can be overkill. I'm a big fan of the ones like CodeIgniter, and a 'roll-your-own' approach where you can have a small core, and leave out all the bloat if you don't need it...
Joshua May said on 24 Oct 2007:
Yeah, I gave a talk on PHPMelb last month about symfony. I'm a huge symfony evangelist, and I think it's the best tool for a lot of jobs in the PHP world.

That aside, I don't really care what framework you use. I prefaced my talk with this specifically. I just encourage people to use *something*, or at least evaluate and justify properly why you're *not* using one (on new projects, etc).

My last workplace had their own in-house framework, which kind of counted as a framework. There was no doco, and the former developer(s) either didn't work there or were offsite. I was in the deep end, and I was over my head. At least was an off-the-shelf framework, I'd have a chance of surviving (well I *did* survive, to an extent - it could have been better though).

My major gripe is how willing PHP developers are to reinvent the wheel. It's just such a waste of time and resources. I assure you, in the majority of cases, someone else has already done it, and someone else has done it better. While we're wasting our time re-inventing the wheel, PHP will gain little traction in Australia - .NET and Java will continue to prevail.

Sad, but that's where we are these days. Oh well. One day things might change.
Binh Nguyen said on 21 Jan 2008:
I wonder what framework you are using for you blog. It doesn't look like WordPress to me. Mine is WordPress and I'm not very happy with it.

I also looked at Drupal and that require me to do too much tweaking before I can use properly.

Regards,
<a href="http://binh.name">Binh Nguyen</a>

Add a comment! Only your message is required (and proving you're human, sorry). No HTML.

 Captcha image - sorry!