PHP Frameworks – make sure it’s the right time
by Richard on 25/08/2011I recently met up with a few good friends from back home in Northern Ireland for a stag do in Liverpool and got talking to a particular friend who’s also a web developer. The conversation eventually turned to shop talk which while including many things got me musing on one topic in particular, PHP frameworks. It got thinking of my framework of choice and more generally the emergence and increasing popularity of frameworks within the industry on my drive back home. Is this reliance on frameworks good or bad? Does one miss out on anything by using a framework? Are they really all they’re cracked up to be?
I don’t intend this to be a long article. In fact I don’t intend this to even be an article that has a particular purpose, say to sway your opinion on anything in particular. Instead I merely wish to discuss how I view the current crop of frameworks and when the right time to use one is, that is if there is a right time.
So let me kick of the meat of the discussion with the question ‘Is a PHP framework a net benefit to the developer who chooses to use one’? I’ll do my best to explain why I think the answer is a resounding yes, albeit with a caveat or two.
As with anything there are advantages and disadvantages. Trade off’s which you have to decide are worth it or not. As I’m feeling in an optimistic mood tonight, most likely due to the choc fudge cake and ice cream I’ve just indulged in I will begin with the advantages.
Temporal Efficiency
How about that for a headline! Who wouldn’t want temporal efficiency? Ok maybe I’m over egging this bit but then again maybe not. Time is the most valuable asset we have. Anything that increases the amount of it we can productively use can only be a great thing.
Semi nebulas statements out of the way. Basing your next project on a framework can save you a lot of time throughout the projects lifetime. All the major projects come with well tested and sensibly implemented classes and helper functions that as much as possible allow you to work on the logic to solve your clients needs rather than implementing the basic workings of your system.
For an example. A client of Thirst currently have a website which is getting pretty decent traffic sense we redeveloped it for them. When we had redeveloped it we put it on Media Temple’s Grid hosting, I fully expected to have to move it off this pretty basic hosting as soon as their traffic started increasing but as I had built it on CI (codeigniter) it turns out I didn’t have to as it handled the traffic pretty efficiently. Now here’s the great thing. When the website does start pushing very close to the limits of the Grid hosting package it will be a relative cinch for me to turn on static caching of the sites dynamic content and prolong the time that I can keep the site on the Grid. This is great for the client as they get to stay on a very good value hosting plan and keep there costs down as long as they can.
Now I’m not saying that if I hadn’t built the site on a framework that I wouldn’t be able to implement static caching of the websites content. I would be able to implement it but the fact the CI has everything needed already in place and all I need to do is make a few method calls makes it all the easier for me to do and cheaper for the client as well.
Another example of a common task that a nice helper function can help out with day to day is DB backup. The previous site I was discussing has outgrown the native PHP DB backup method but most sites probably won’t. Therefore CI having a nice easy way to help me backup a datbase manually or via CRON is going to save me time everytime I use it.
A good way to see what’s on offer is to visit the CI user guide and drop down the top navigation menu and check out what’s on offer.
Tell me honestly if all that well tested and proven functionality doesn’t tempt you.
Code reliability
Maybe you’re a lone developer or one in a team of developers but can you truly say to yourself that you are better than a global network of hundreds or thousands of developers all putting in work to make one system better and better?
I don’t think any sane developer would say they could match the above statement. Not an honest one anyway. It’s just a fact of coding that more good developer eyes means more reliable code.
Sensible and proven design pattern
Model – View - Controller or MVC. It’s a great design pattern for your web apps.
Essentially what it says is that you have your models to interface with your data sources, which are not only a relational DB but could be files or other web services.
Your views are are of course the files that you use to present all this data you have to your users. Note the view may include some basic programming fundamentals such as iterating over an array of something with a loop but they shouldn’t be doing any ‘real work’!
Lastly your controllers are what co-ordinates everything. They are the files where you call your models methods, get the data, maybe doing some manipulating with it and then pass it to your views for presenting.
While there are different interpretations by developers on exactly where (and how much) to place certain logic within the MVC pattern, such as fat/thin models (i take this view) or fat/thin controllers, the consensus on the overall pattern is extremely positive.
This being the case MVC is the pattern that the majority of PHP frameworks use and it is a pattern that will benefit any developer who takes the time to understand it and become proficient in its working.
Another great advantage to MVC is that it really helps you keep your code modular. I think now is a good time for a cheesy video – see below…
Community support
Unless you create your own framework or choose to implement one that is just starting off or that has no support then when you get that client that comes to you requesting something out of the ordinary and you thinking gosh can I do that. Can the framework do that then you can ask that question to the community of users of your chosen framework.
One reason I love CI is that their user guide is very clear and helpful but if I were to ever encounter a problem where the information wasn’t present in it then I would feel very comfortable asking some help of the wider community.
Support if the community is a great asset to have. It is one that you don’t appreciate until you have had to lean on it in a time of need. I can safely say Ci has a great community but I would wager that it is closely match in this respect by almost all of the popular frameworks.
Performance
The guys and girls and Yii framework have a chart detailing the performance of various frameworks. Of course Yii wins! I don’t mean to be cynical, Yii is actually built to be the fastest and I think the consensus is that it does that part of the job well.
I included the above link to let you see a snapshot of the comparative performance of the major frameworks. I’m happy to see that CI is the best par Yii for performance but I think it could be even better. You see you can config CI to only load what you need. So if your app will never access the database just don’t load up that class.
Performance isn’t everything but when choosing a framework do take it into consideration. Your choice is to suppose to help you build and maintain your web apps easily and as efficiently as possible but you don’t want your framework giving with one hand and taking away with the other one.
Now that the advantages are out of the way and the choc fudge cake has given me a headache I shall discuss the disadvantages of using a framework.
Learning curve
With any approach there is a learning curve. Hell with learning native PHP there is a learning curve!
It is often brought up as an argument against frameworks. While it is true it isn’t anywhere near reason enough to not use a framework. I think it stresses the need for a developer to properly research his options before choosing a framework as he/she doesn’t want to be continually learning new ones or switching between 10 different ones.
Proprietary solutions
I say proprietary but the truth is that’s not really accurate. Sense all these frameworks are open source there’s noting proprietary about them. It would be better to think of this possible slight disadvantage as ‘system specific solutions’.
By that I mean the way of developing within CI will be different to CakePHP or Zend.
A developer must be aware that if they invest in learning a framework then they are taking a slight risk in that if it were to disappear part of the time invested would be wasted and they would need to either swear off all frameworks or choose another one to develop with.
Lack of true understanding
This is the only one I’m honestly worried about when chatting to people regarding frameworks and my opinions on them.
If you are a developer starting out put in the man hours to understand how PHP actually works. Use it’s native functions, read up best practice articles on how best to develop with only basic PHP. Don’t use any frameworks for a while. When you are very comfortable with native PHP then start looking into frameworks and ORM etc but get the basics first.
I would hate to see a developer who didn’t know what was actual native PHP and what was part of the framework. Don’t let that be you!
Sum up
I will say that if the developer is careful and aware of the risks then said risks become far less potent. It is the developer that isn’t aware of them that will encounter problems in the future.
So should I use one!
Here’s the straight forward answer. Yes. Be aware of the downsides if they apply and then jump in with both feet.
If I’ve got my points across with any clarity at all you may well be looking for a list of popular PHP frameworls right about now. Well I’ll be damned, look at that!
- Codeigniter (My chosen framework)
- Zend (Sort of the official framework but quite large and a bit slow)
- Symfony (sorry I’ve no experience with this one)
- CakePHP (recommended by a friend I respect but I didn’t like it)
- Many more…
And in case you want a bit more on MVC and haven’t already jumped away to another site check out this Stanford video lecture at the 22:30 to 28:00 minute mark for a quick walk through on MVC.
Enjoy all your future framework-y goodness!
richard@richardjohnashe.com
No comments yet.