Archive for December, 2008

How to learn a new PHP framework

Wednesday, December 10th, 2008

There are dozens of PHP frameworks around now, some attracting more attention than others. I am no expert on these frameworks and have not used a single one extensively so I wouldn’t dream of recommending one over the other, nor do I want to enter the debate about what is or is not a framework (I’ll work on the assumption that anything calling itself a framework is a framework). Instead I’m going to concentrate on how you can get started with a new framework as quickly as possible.

First you have to choose a framework; there’s ZF, Cake, Symfony, atk, Yii, CodeIgniter, Solar, PRADO and so on. But which to choose? Some of these offer some really good step-by-step tutorials, but for others, they can be hard to get into.

I really like the idea of exercises for learning a new programming language, but I (and probably you) already know PHP fairly well, so none of them really apply when learning or evaluating a new framework. I wanted to create a similar set of exercises that I could apply when looking at the multitude of frameworks available.

If you’re looking to get your head around one of these things, and don’t know where to start – why not give these exercises a try:

1. hello world
This should need no explanation. Build an app in the new framework that prints ‘hello world’ to the page. This might not be as straightforward as it sound.

2. calculator
Write simple calculator that takes two numeric values and an arithmetic function (add, subtract, multiply and divide) from a form and then prints the results.  You should make each different arithmetic function its own action/method/whatevertheframeworkcallsit and you should use the frameworks input validation for the form values (if it has one).

3. guestbook
Go back to 1998 and build webpage “guestbook” script to display a form which a user can complete, and submit to the app, which then saves the form content (to a db/file/whatever persistence mechanism you desire) and displays the contents of the guestbook on the web page. If the framework supports creating the HTML form/object model/persistance layer then make sure you use that here.

4. parse and paginate
Find an RSS or some other XML feed that contains lots of items, parse it, and crap out the items in the feed. Add pagination so a user can read the feed 5 items at a time. Make sure you put the pagination links at the top and bottom of where you’re displaying of the XML items using whatever the framework offers for reusable page elements.

By now you should be starting to know your new framework pretty well, and be ready to undertake a more serious project and explore some of the other features your framework offers. If you still don’t get it, or running through these took you unacceptably longer than it would if you just wrote the all the code yourself from scratch, then the framework you’re learning probably isn’t for you, so put it down and try something else.

Remember: ask for help from the community surrounding that framework if you get stuck. A framework with a strong community will mean you can get help in future when working on more important projects and secondly, its a really good indication of the health of the framework – there are few things more irritating than baking your application around a 3rd party component (like a framework) and finding out six months later that the developers have given up maintaining thier project.