Why You Should Be Using a PHP Framework
All frameworks discussed in this post are full stack web frameworks coded in PHP such as Code Igniter, Symfony, Kohana, Yii and CakePHP. Some (but definitely not all) of the points below may apply to micro frameworks such as FatFreeFramework, Limonade, Glue and Slim.
When should I use a framework?
Frameworks should be used when constructing web applications. Any application that involves a database, forms, sessions, cookies or a remote service (such as Twitter or Facebook) will benefit from being powered by a framework. There is no need to use a framework for a website that has only one or two pages, nor for command line utility scripts.
What features do frameworks provide?
- Database abstraction. Frameworks can abstract away SQL-vendor specific features. This allows you to change your SQL database without needing to rewrite any code. For example, switch from MySQL to Postgres or MSSQL. Some frameworks can also handle database relations automatically, which saves you having to write JOIN queries.
- Cache abstraction. Rather than using backend specific caching functions (such as apc_add and apc_fetch), you use a generic caching class which has support for multiple backends such as Memcache, APC and XCache.
- Form management. Symfony2 and CakePHP allow the developer to define forms as PHP code. The framework then handles all HTML widget generation, form validation and CSRF security automatically.
- Authentication. Most frameworks come with a generic user authentication module. They handle logging in and logging out, registration, session management and permissions. They are typically very easy to modify and add your own custom fields.
- Easy Debugging and Profiling. Kohana3 and Symfony2 come with debug toolbars. These allow you to inspect global variables, database queries, logs and load times in the current request.
- Internationalization. Built in translation frameworks let you easily create country specific versions of your website.
What benefits do I get from using a framework?
- Portability. Database and cache abstraction allows your application to be used on many different server setups. If your application is open source, more people will be able to install it.
- Shorter development time. Because your not writing the same boilerplate user authentication, database and form code, development time is shortened dramatically.
- Application security – Security features such as authentication and permissions are handled for you by the framework. Database inputs are automatically sanitised and most frameworks have cross site request forgery (CSRF) protection built in.
- Community supported – Frameworks have forums, mailing lists and IRC channels supporting them. If you encounter a problem with the framework, chances are someone else has and a fix has already been made.
- Plugins and modules. Members of the community release plugins and modules which you can download and use in your application. These would be things such as integration with 3rd party APIs (Facebook, Twitter) and features such as template engine integration.
- Enforces good coding standard. Most frameworks force you to follow the Model, View, Controller principle. This makes you (the developer) think about how your code will be structured before you write it, making it of higher quality.
- Future proof your application. Frameworks are well documented and tested. If another developer was brought in on the project, or you sold your project, the new developer would just need to read up on the framework documentation and then would understand the code. If a framework wasn’t being used then time and money would need to be put into training the new developer.
What framework should I use?
Everyone has their own opinion on which is the “best” framework. I recommend you have a look at all frameworks linked below, they each have different styles of structuring the application. If you have experience with Ruby On Rails then you may want to look at CakePHP and Trax. They both have ActiveRecord implementations and model themselves after Rails. QPHP will seem familiar for those with ASP.NET experience and Symfony2 will make people who know Java feel at home (Symfony2 overview). Also be sure to check out Kohana, Code Igniter, Yii, Lithium, Fuel, Alloy and Akelos before making your decision.



April 11, 2011
hi
I’m just playing the devil’s advocate here (I’m in favor in using frameworks, be that either a full fledged framework like Symfony, or a component based one like Symfony2, even a no-framework framework is better than nothing), but many of the features and benefits that you mentioned only work in theory, not in real life.
usually you can’t just replace a major component in your application (like a framework or a database or cache backend), because the abstractions are leaking:
http://www.joelonsoftware.com/articles/LeakyAbstractions.html
Tyrael
April 11, 2011
You forgot some of the newer PHP 5.3+ frameworks in your list. They are definitely worth mentioning in your post: Alloy, Lithium and Fuel. I am sure others will follow in the comments as well.
April 11, 2011
You might have forgotten Zend Framework.
April 12, 2011
@vlucas
Thanks. I’ve added them to the list.
April 12, 2011
@Tyrael
Look at Drupal, phpBB, IPB. They can all run on a variety of SQL databases with a variety of caching backends. I agree though that some SQL database specific implementations such as views, triggers and procedures are almost impossible to abstract, luckily the majority of applications do not need them.
April 13, 2011
Hydrogen is another PHP 5.3 framework.
http://www.hydrogenphp.com/
April 29, 2011
@logan: yeah, last time I checked Drupal didn’t even use autoincrements/sequences for portability reasons.
which is a good thing for a CMS, but bad thing for a custom application, especially if you aim form customization and performance.
btw: http://breezephp.com/ could also be added to the list.
Tyrael