Friday, June 22, 2012

Why use a PHP framework?

There are several reasons to use a framework instead not using one. The framework that you are using can be one that is open source or your company/team developed for future projects. Learning how to use a PHP framework(or any other framework developed for other languages) can take time depending on multiple factors like documentation, community, practices and patterns that it uses. But it will worth your time because when developing web applications using that framework it will be faster than not using one. 

For example Codeigniter is a lightweight framework that it is easily to use and learn, I mean really easy to learn it. You can start developing a website after a few hours you have downloaded it. But if you have to work on a large project then you should use for example Zend or Symfony2 because these frameworks have more components available to help you with different tasks.

Easier to understand someone else code

 Firstly if you using a framework then other developers who uses it will understand better your code. If developers understand better the code then they will easily extend, fix bugs or use what you wrote. Let's say that you work at a small company in a team of web developers and one of your team member gets married then he has no time to finish the project on which he was working on. Another team member will easily continue his work if he uses the same framework and if the team has a certain coding standards.

If you have some experience with a framework then you will get a job easier because a lot of companies use at least one framework. The most popular frameworks are Symfony2, Zend, Codeigniter, CakePHP. Here is a comparison.

Built in classes that help you

The second advantage of a framework is that you can reuse classes that helps you to get your job done faster. For example the framework can have a database layer that is easier to use for connecting to a database, maybe the framework has a class that helps you to write queries without writing any sql. Of course that in larger projects you must know how to write complicated queries.

Another example is Zend, this framework has a lot of modules that help you to work with different APIs. Zend has a module that helps you to work with Amazon Service. Here is a piece of code example:

$amazon = new Zend_Service_Amazon('AMAZON_API_KEY', 'US',  
$results = $amazon->itemSearch(array(
    'SearchIndex' => 'Books',
    'Keywords' => 'php',
    'AssociateTag' => 'yourtaghere'

Your web application would be more secure

Another advantage is that a lot of PHP frameworks have built-in security. I am not saying that you should leave the framework to take care of all the ways that someone could break your website but your web application would be more secure if you are using a framework.

Build in caching

Most frameworks have build in caching components that will make your web application faster. This components give you the power to control caching in different ways like timing and other things. Here is an article about Symfony2 caching component.

Separating your logic in your web application

For example if you are using a MVC framework and you are separating the logic of application in the three major parts: views, controllers and models then it will be faster to develop, test it, refactor it and understand it. The views should not contain any logic, it should only present data to the user. The controller must receive a request and return an response. And most importantly the models should contain your logic of the application and your database queries. If do not organize your files and your code properly then your project will become a mess and it will be hard to debug it, to extend it and to reuse that code. It will become a spaghetti code!


Learning a php framework can take time depending on which you are learning but the productivity will grow. Frameworks have a lot of components that will ease your work. Inevitably you will learn to separate your files and code in a way that makes sense to you and the others who use the same framework. Almost all PHP frameworks are Object oriented so all of them use the best OOP Patterns to solve common problems. If you want to start building your own MVC framework then you should follow this tutorials.