Back to the blog

RSS

Recent Posts

Most Popular Posts

  1. Why you should be using a framework
  2. Perhaps the world doesn't need another framework
  3. Rewriting URLs with Apache's mod_rewrite and PHP
  4. Five easy things that make you a better web developer

About the Blog

Self portrait

I'm a 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>
Ivan said on 09 Sep 2008:
Well, for small projects I use a custom PHP toolkit. It has all the basics: DB class, Form class with validation, URL class and a fancy auto save/update record class.

For BIG projects I suggest Cake because it saves you a lot of time in the long run.
GogruMogru.com said on 09 Oct 2008:
The best way to make some website in PHP is to code without using any framework because once you start using frameworks, you can never code wihout frameworks.
tech news said on 21 Nov 2008:
Rasmus said dont use framework as they are slower then a simple php.
mihai said on 14 Jan 2009:
i use CakePhp. It saves me for alot of headeacke. U can see it runing on www.revistacioburi.ro
Paul said on 08 Feb 2009:
I am looking for framework that will give me possibilities to customise as i wish and max possibilities (e-commerce etc.).... I am a new developer and I dont what to jump form one to another etc.

I dont like to code to much as well... ;/

You think CodeIgniter will be the right solution for me?
Nic Appleby said on 10 Feb 2009:
An interesting blog. I really like codeigniter myself as i find it to be very lightweight, yet easily extensible.

I was wondering if you have any metrics with regard to performance of straight php vs a framework, or at least any comments or thoughts? How important is it that straight php outperforms most frameworks?
Robert said on 05 Mar 2009:
I've got hired on as a entry-level php programmer in October of 2008, and from the start have I not only been slighty over my head in everything I've been doing (working within a pre-existing OO code base), but I've also been editing and creating all my code in Text-Wrangler for Mac, where the biggest feature is really just the function browser =P. I'm wondering what you would recommend I use as a framework? I'm feeling the time is now to start developing good habits before the bad ones are set in too deep.
Michael said on 22 Apr 2009:
Hey Robert - you probably won't ever read this, but I hope my message can help other that visit this entry later..

It's hard to recommend anyone a framework, and even more difficult to get a straight answer from anyone, because they'll either recommend their framework with extreme bias because it's the only one they know. Heres my take from my last few weeks researching different frameworks:

CI (Code Ignitor) - Documentation is really crisp and easy to understand, not as powerful as others, but good for beginners to MVC and OOP.

Zend - Don't know too much about, but seems like it has a alot of Libraries built in, so it's going to be pretty powerful, but I also understand it has a steep learning curve.

CakePHP - Pretty large community, good resources, and I've found more tutorials on Cake than other frameworks. It's very MVC strict, whereas CI is very lenient (You don't have to use models in CI I believe). Encourages good code practice and agile development.

I ended up (after much debate) to go with CakePHP, seems most popular so there are a ton of people that will help you out with what you need. I'm extremely new to OOP (pretty much just grasped the basics last week) and my very first time experiencing MVC and so far I'm working well with it. CakePHP for me is really only the starting point to get me into an Object Oriented and MVC mindset, then in a few months after a couple applications, I'm switching into Ruby and (of course) rails. I have a few friends in it and they praise it, so that's my direction.

Hope that helps anyone, I've only been around back-end programming since September, so about 8 months of Procedural development, and I'm getting out. Cheers. Feel free to email me if you have any questions as well michael ]at[ thisiswilson ]dot[ com

Cheers!
Zeck said on 01 Jun 2009:
Hi, I'm student and developing own php framework. It named ZooZ and i absolutely agree with you.

Thanks.
Sorry for poor english.

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

 Captcha image - sorry!