How to learn a new PHP framework

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.

26 Responses to “How to learn a new PHP framework”

  1. Rushi says:

    I’d like to propose an addendum to #3. “Use Ajax”

    A lot of frameworks have Ajax support built in and in today’s world it would be good to know how well your framework supports it.

  2. AppBeacon says:

    I’ll play the devil’s advocate.

    You should also consider whether spending the time learning a new framework would be better spent learning your language of choice. Learning a new framework is not going to make you a better PHP developer. It’s going to make you a better Cake or Symphony or Yii developer.

    When you’re going for a great job, MOST places don’t care how well you know Cake. They want to know if you can jump in with existing code, pull data out of a database, and display it nicely.

    I’m not saying frameworks aren’t useful. However, they make it easier to code by not having to know as much about how a language really works and keep you from learning that language better to a degree.

  3. rzei says:

    Since having turned from php programmer into the big Java world with Spring and Tomcat I haven’t looked back since the beginning of the learning curve. (While in the beginning of the learning curve I screamed for php easiness, but later on I do not regret my choice.)

    I still believe that php has it’s place but when you really need to use Symphony or Cake for your _client_ I believe you’ve gone wrong right there. If you are getting payed good, why not provide your client with something with higher performance and quality?

    Quality doesn’t come free though, while I do think that all Java’s annoyances (packages, strict file placement, one class per file) will eventually guarantee that your code _will_ be more readable in the future. Use of other advanced technology like Spring yields much better code through programming of POJOs against interfaces.

    Disclaimer: I do not mean to troll, but first seeing the topic and then reading AppBeacon’s comment finally made me to share my experiences as a fellow web-developer.

  4. Dave says:

    Some useful tips and interesting comments. I think it is good for any programmer (regardless of language) to look at various frameworks from time to time. This gives you the opportunity to look at the code and to see how other people have handled a particular problem. It can be a very useful learning experience as well as exposing you to alternative methods and thinking.

    @AppBeacon: agreed. Being versatile is definitely a bigger advantage than being a “Cake Ninja” or “ZF Guru” for example (unless that IS what the company wants ;) )

  5. CLee says:

    @AppBeacon


    However, they make it easier to code by not having to know as much about how a language really works and keep you from learning that language better to a degree.

    I think that’s probably one of the most important reasons people use frameworks. It’s more productive by writing less code and it’s proven by many others. I think many places care about how good you are at Cake or ZF. Why is .NET so popular? As a management’s stand point, it’s easy to find a developer who know .NET and can take over a .NET app.

  6. Marc says:

    How about using a decent editing tool like Eclipse? (Or any other tool for that matter) It’ll give you some helpful tools figuring out what all the classes do and what inherits what…

  7. Stuart says:

    I’d suggest something more abstract. Some of the core areas to learn in a framework are (IMHO and in no particular order):

    1. How to handle authentication and authorization (and how to persist and retrieve this data)

    2. How to handle database manipulation (CRUD at least)

    3. Creating/filtering/validating forms

    4. How to use the framework’s basic templating system

    5. How to access the data being passed by the framework’s various objects (requests, responses, registries, etc.)

    6. Pagination

    7. Implementing search

    8. Overall configuration possibilities for the framework (xml, yaml, ini files, etc.)

    9. Integrating AJAX

    10. Translation

    11. Handling exceptions

  8. Phillip says:

    Stuart is spot on. I recommend removing your weak points 1-4 and putting in Stuart’s points 1-7. Rushi, AJAX is a client side technology. PHP frameworks should support all the AJAX libraries as templates can output XML and JSON as easily as HTML. Choosing an AJAX library is a whole other independent subject. Translation is complex and no framework in any language has it perfect. I would actually push Stuart’s point 4 up to the top of his list as it affects the ability to generate quality content. If it’s too restrictive and you have to start significantly modifying it then you may as well have written your own framework in the first place.

    Phillip.

  9. bob says:

    Frameworks are only good if you know how to do the same work without the framework. Just thought I’d throw that in there.

  10. Marc Gear says:

    Stuart,
    I wanted to get away from a list of responsibilities that a framework should handle, and create a set of simple no-brainer apps that would allow a user to explore the framework and its intricacies and differences from what else is available.

    Personally I find working from the abstract lists of functionality offered by frameworks in most framework documentation a very slow way of learning. I believe that the best way is to build real apps, exploring the documentation and features of the code as you go. As you’ll appreciate, building the apps I suggest will cover most of the areas you mention.

  11. Silvio says:

    Stuart, what PHP framework you think implements this features? (or near)

  12. Stuart definitely has the right idea. It’s that exact set of questions which led me to write my own framework 4 years ago instead of using an existing one, because there was no framework that I could easily *tell* did all of these things well 4 years ago due to lack of documentation and examples.

    In fact, the “home” page about my phocoa framework lists exactly that set of questions a solution areas. (Except translation; haven’t tackled that one yet).

    It would be nice if there was a “standard” evaluation example and all of the PHP frameworks had a tutorial on that exact project. This would make it much easier for people to compare the frameworks to see which framework’s style and architecture best suited them.

  13. Stuart says:

    First off, I agree with Bob that you need to know how to do what the framework does or you risk serious confusion. An example: with the Zend Framework you can populate form fields from a database very easily (literally just $form->populate($data) assuming your table keys match the form field names). However… put a select element into that table and it won’t get populated. You need to know that the framework expects an array to populate a select which means you need to be able to understand and take care of that on your own. In my instance that meant making a database call from within my form’s (which extends Zend_Form_Abstract) init method, flatten the array (I didn’t want a multi-dimensional array), assign the value to the key (so that I had array(1=>1,2=>2) rather than array(0=>1, 1=>2) because the key would be interpreted as the select’s value while the value would be displayed and I needed them to be equal) and then pass that array to the select element. The framework doesn’t help with this at all (and there is nothing in the manual about this… that is ZF’s biggest shrotcoming in my opinion) so you need to be able to figure it out yourself.

    @Silvio – I personally prefer the Zend Framework. It is flexible and let’s me work in a way that is more familiar to me… makes “learning the framework” less of a chore.

    @Alan – I agree. I don’t think I’ve ever seen a tutorial or set of examples that went in depth very much at all on something like this. They usually start of with “We’ll make a super simple…” (i.e. basically useless), have a “one, two skip a few” set of instructions or use some extremely programmer specific techniques that make what should be “standard” into a pretzle of “why’d he do it that way? Wha…?”

  14. terry chay says:

    I like this idea (doing the same app in different frameworks). In fact I think I’ll try it. I only did hello world and then jumped to twitter-like app… which was a problem.

  15. Frankos says:

    Try writing your own framework.. You will definitely become a better PHP programmer. It makes you realize the advantages of:

    Breaking your website into modules.
    Why object orientated programming is so powerful.
    Why HTML just gets in the way.
    Why writing forms, form validation and using POST’s are just a repetitive boring process.
    Why having your own framework gives you the alter mate control.

    I don’t have to write any HTML at all now, the framework objects do it for me.

  16. .:; I am really thankful to this topic because it really gives great information :-:

  17. [...] a sample app)I was searching for the same on google and I landed upon this comment in this article (http://onekay.com/blog/archives/35) which lists a bunch of stuff of to do with any new framework in no particular order of [...]

  18. [...] a sample app)I was searching for the same on google and I landed upon this comment in this article (http://onekay.com/blog/archives/35) which lists a bunch of stuff of to do with any new framework in no particular order of [...]

  19. Tragamonedas says:

    schöner bichenpla die kackt den freilalt ohne spieren. ebesfuba mir ftigkei die ausgenk und enammert den ventzwei und sprietion den kormerben mit hmenin.

  20. Kartenspiele says:

    excelente a anteco si ltubalha rentogra con adento. peges istados se ssenderir son stamo mi copinto rendo y prias duria didizair.

  21. [...] Marc Gear states some of the exercises that we should put our hands dirty at: http://onekay.com/blog/archives/35 [...]

  22. Allene Bocek says:

    Danke für diesen interessanten Beitrag. Jedoch ist dieser Post schoon ein paar Tage alt, dafür aber immer noch sehr aktuell. Ich würde mich freuen, wenn noch mehr solche Beitrag veröffentlicht werden.

  23. F*ckin? remarkable things here. I?m very satisfied to see your post. Thank you a lot and i’m having a look forward to touch you. Will you please drop me a e-mail?

  24. breeding says:

    Howdy! Do you use Twitter? I’d like to follow you if that would be okay. I’m undoubtedly enjoying your blog and look forward to new updates.

  25. not because you want to do too much exaggerated to cover up defects the so-called overkill,mens uggs what should be just right, but not too far.

Leave a Reply