Choosing the best PHP MVC Framework – Part 1
So, perhaps like me you are writing a fair bit of PHP and building some serious PHP apps. Perhaps like me you have also decided that re-inventing the wheel for every app you write doesn’t make much sense and PHP by itself isn’t quite at a high enough level to be considered a framework. I mean PHP is nice and all and there are fast ways to do many things in PHP, but there are far too many ways to do things in PHP and all you need is just one that works. So having come to this realisation you decide that what you need is a PHP MVC Framework.
Why a PHP MVC Framework? Well to be honest the PHP Framework doesn’t need to use MVC as such, and you probably don’t even need a PHP Framework, just good PHP programming practices where you are separating the presentation (i.e. the View) from the business logic (Model and Controller). You also want a standard way to do things like output common presentation elements (like forms) and do things like perform form validation, access the database and so on (I’ll cover this separately). Whether you decide to roll your own (something I’m considering for reasons I will discuss later) or use an existing PHP MVC Framework, you still need some sort of a framework, as otherwise you’ll be writing the database access code for every app and that just doesn’t make much sense.
So, you realise that you indeed need some sort of a framework. The next question is what framework. For now I’ll concentrate only on PHP Frameworks but will at some stage discuss other technologies such as ASP.NET Forms and ASP.NET MVC as that’s a topic of it’s own (i.e. PHP MVC Frameworks vs ASP.NET MVC).
Through your research you’ll find that there are MANY PHP Frameworks available, and indeed I dare say a few TOO MANY. Most of these frameworks are MVC Frameworks. The reason for this is that MVC is a well understood and accepted software design pattern that aims to separate the presentation from the business logic (and presentation specific business logic (i.e. Controllers). To be honest in PHP, although some may argue otherwise separation of the Model and Controller probably isn’t all that important for a simple app where you don’t use the same model in multiple conrollers, but it does become important if you’re looking to re-use the model in multiple controllers.
It’s also worth quickly covering off what the Model actually is, although that probably deserves a post of it’s own as it’s so mistunderstdood. In broad terms the Model is really just Domain Logic, or code that isn’t (or shouldn’t be) tied to a particular user interface. In case of PHP the different front ends may be your HTML based UI and XML/SOAP based Web Services, both of which should use the same model, and hence the model shouldn’t really have any UI specific logic. For example if validation is done in the Model (which is where it belongs) then the model shouldn’t just output HTML when an error is encountered as that’s specific only to HTML. This concept is much more evident in .NET Applications that have both an ASP.NET front-end and an Windows Forms front-end and often a Web Services interface.
Anways, you’ll probably find, there’s quite a long list of MVC Frameworks (see PHP Frameworks and Wikipedia although PHP Frameworks is the better source)… Here are some of the best known ones and my brief thoughts on them… I’ll cover each in a separate post.
- Zend Framework – A well known enterprise level php framework, with a an MVC component, very complete and cohesive, but also very performance heavy. Unless you have a Dedicated Server, a VPS or at least opcode caching on your shared hosting it’s probably not a good idea. Also it’s a glue framework, so it won’t force you to do things in a particular way, which can be both good and bad.
- Yii Framework – A relatively new PHP framework, but well architected and feels very similar to ASP.NET in some ways. This is one I felt most comfortable with. It is strictly PHP 5 and Object Oriented. It uses a class loader to be as efficient as possible, but isn’t superfast. Fast growing community.
- Kohana PHP Framework – An offshoot of Code Igniter that is strictly PHP5 and Object Oriented. Quite a nice framework, but doesn’t feel quite as nice as Yii… Performance is similar to Yii. Pretty big elite community.
- Code Igniter – Very popular PHP MVC Framework, with a massive community, but still supports PHP 4 and as a result has code for interoperability, thus slowing things down a little for no good reason if you’re not using PHP4. Doesn’t feel quite as nice as Yii or Kohana.
- Solar PHP – A relatively new recently released PHP Framework. Doesn’t have much of a community behind it and is pretty much a lighter version of Zend Framework. It’s only at version 1, so not sure you want to take the risk.
- Doo PHP – A fairly lightweight PHP MVC Framework, that claims to have the best performance of all PHP frameworks. Although that may be the case it forces you into using ORM and just doesn’t feel all that natural. Even class names are weird… Seriously, what does SqlMagic actually mean?
- Symfony – Quite a cohesive framework, but slow so best only left to enterprise development, in which case Zend is probably a better choice.
- CakePHP – Quite a popular MVC Framework but slow and also forces you to do things its way.
- ezComponents – An older framework, somewhat similar to Zend in terms of being a component framework, also relatively code heavy.
- Seagull – Build on PEAR, which is more a component library than a framework, havn’t really looked into it much, so can’t really comment.
- Ash.MVC – A very basic PHP MVC Framework that does nothing but MVC itself and DB access. Not something you’d consider writing your serious apps with.
- Qee PHP – A chinese PHP MVC Framework, unless you speak chinese it’s probably best to steer clear.
- Think PHP – Another chinese PHP MVC Framwork, so unless you speak chinese, probably best to steer clear
With so many frameworks available you really need to work out how you’re going to work out which is the right framework for you… I will exlore this in more detail in the following parts of this post, but briefly, the things you need to think about are:
- Performance – This depends what you’re developing, but you really don’t want a framework to make your website slow if starts getting some serious hits. Although this needs to be balance off against other factors.
- Cohesiveness – Its best for a framework to be cohensive rather than just a few different components thrown together out of different places (e.g. PHP Classes). This will make it easier to develop with.
- Comprehensiveness – You don’t want to end up with a framework that can’t do many of the things you need to do in day-to-day development or you’ll have to go looking for solutions. For example, very few frameworks atm have a HTTP Client class!!!
- Feel – It’s important that a framework feels natural and that class names make sense and so on as it will make your development faster. Also you don’t an MVC Framework that forces you to use ORM or Active Record, as that’s not something you absolutely must or even should use.
- Documentation – You don’t want a framework that’s undocumented so you’re forced to go wondering through code to work out how things work. Its good to have lots of examples.
- Community – It’s pretty important for a framework to have a large community as that means that someone will most likely have come across and solved the problem you’ve come across.
- IDE Support – You want to make sure that the frameworks works in a proper PHP IDE like Eclipse PDT and NetBeans (I’ll post on this separately). Having the framework with proper comments on classes and functions makes development in an IDE much easier. Also it’s best to use a framework that doesn’t use the Registry for absolutely everything or in such a way that type inferencing and code completion doesn’t work.
- Time Around – An older framework will tend to have a larger community, fewer bugs, solve more of the common problems and so on.
- Interoperability – How easily can the framework work with other frameworks, classes and so on, in case it doesn’t do what you need it to already. Is there an easy way to extend it?
So when comparing frameworks you need to take all these aspects into consideration.
Personally after spending a fair bit of time on the frameworks I narrowed my choice down to:
- Yii Framework – Because it feels most natural and is quite comprehensive, although I don’t like that it pretty much forces you into Active Record and ORM and isn’t very performant.
- Kohana PHP – Because it’s still strictly PHP5 and OO and hence doesn’t need interoperability code like Code Igniter. However, some things are a little overdone, which impact perfomance. and some bits don’t feel natural. e.g. class naming for static classes (e.g. date_Core).
- Zend Framework – Although it’s a bigger beast it is the most comprehensive and with the biggest community. I won’t dare using for an app to be hosted on shared hosting though as it’s too low and heavy for that.
- Code Igniter – It’s lightweight and all, but I don’t like the PHP4 interoperability and not being OO, althought that doesn’t really matter so much as a static class and just functions is much the same thing.
There’s actually a great post on Beyond Coding that comparing Yii vs Kohana vs CodeIgniter. It considers most of the aspects I’ve gone over but doesn’t really look into performance (as all those frameworks perform in the same ballpark).
There is also this other pretty good post comparing CodeIgniter vs CakePHP vs Yii.
In the future posts in this series I’ll expand on the criteria I used to evaluate the PHP MVC Frameworks and then also compare specific frameworks. I’m also going to look specifically at framework performance as that’s absolutely paramount.