PHP Frameworks have been receiving a lot of attention in the past few years from the web development community. What’s this hype all about?
If you look at PHP Job listings, you will often see “MVC Framework Experience” as one of their requirements. It is becoming one of those must-have skill sets for web developers.
There are pretty good reasons behind all of this. In this article we will explore why you, as a PHP programmer, should be using a PHP Framework.
1. Code and File Organization
Just because you created an “/inc” folder and made a “functions.php” file does not mean your code is organized.
When you setup a PHP Framework, it already has a certain folder structure. It is expected from you to follow the same standards and keep everything organized in a certain way.
Once you get used to this model, you will never want to go back.
Unfortunately for some command line champions that still use vi, this can be a challenge. You will need to work with more files, that are smaller in size. But when you use a decent modern code editor or an IDE, it will be a breeze to browse through your application code and find what you need, quickly.
2. Utilities and Libraries
PHP is a great language for web development and provides countless number of tools and libraries.
However, if you ever try to build a whole website with PHP alone, you will find yourself either hunting down a lot of 3rd party code and libraries, or have to write them yourself.
All top PHP frameworks come with certain Libraries and Helpers, that help you with:
- Form Validation
- Input/Output filtering
- Database Abstraction
- Session and Cookie Handling
- Email, Calendar, Pagination etc…
The list goes on. Not to mention, there is plenty of plugins provided by the community that you can add to your framework.
3. The MVC Pattern
The famous Model View Controller Pattern dates all the way back to 1979, when a guy named Trygve Reenskaug (a Norwegian computer scientist) first described it.
PHP itself works like a template engine. However, when used irresponsibly, it leads to very ugly and unmaintainable code.
The way the MVC Pattern applies to PHP applications:
- Models represent your data structures, usually by interfacing with the database.
- Views contain page templates and output.
- Controllers handle page requests and bind everything together.
This kind of separation leads to cleaner and more maintainable code.
In PHP you can already find many input and output filtering functions to protect your website against certain attacks.
However, manually using these functions can get tiring and you may forget about them every once in a while.
With a framework, most of the work can be done for you automatically. For example in CodeIgniter:
- Any value passed to database object gets filtered against SQL injection attacks.
- All html generating functions, such as form helpers and url helpers filter the output automatically.
- All user input can be filtered against XSS attacks.
- Encrypting cookies automatically is only a matter of changing a config option.
5. Less Code & Faster Development
There is of course a learning curve for all PHP Frameworks. But once you get over this hump, you will enjoy the benefits of rapid application development.
You will write less code, which means less time spent typing. You will not have to chase down 3rd party libraries all the time for every new project because most of them will come with the default framework install.
Also, since you are being more organized, it will be much faster to chase down bugs, maintain code, and make changes to existing code.
6. Community Support
All popular PHP Frameworks have great active communities behind them. You can talk to other developers, get help, feedback and also give back to the community yourself.
There are message boards and mailing lists… You can also learn a lot by just browsing the forums and look at what other people are talking about.
7. Job Opportunities
Have you looked at any PHP Job postings lately? Most of them require experience with either Frameworks or a CMS. Follow the demand!
Example from Careerbuilder.com:
Experience with PHP Frameworks greatly increases your job qualifications as a web developer.
8. Performance Tools
One of the main arguments from the naysayers comes in this subject. There is obviously a performance hit when you build a “Hello World” application with a framework vs. plain PHP code.
But those benchmarks are just bad examples. First of all, you should understand that developers are more expensive than servers. Saving time from development and maintenance is likely to outweigh any extra money you need to spend on servers.
Putting all of that aside, you can actually gain performance benefits by using a PHP framework. They come with tools that help you do caching, benchmarks, profiling etc…
Modern frameworks are also great with dynamic loading, as they load code only as needed. Different page requests can load different amount of library code based on what needs to be used.
9. Suitable for Teamwork
The way your project is organized in a PHP Framework also helps you create a suitable environment for teamwork.
You can let your designers work on the Views, database guru work on the Models, let the smart programmer (yourself ) build reusable Libraries and Plugins etc…
Also you can let someone build unit tests, because they come with tools for that too.
10. And It’s Fun!
This might actually be most important point of all. When you have fun doing your work, you will be more productive and happier in general.
If you have been coding plain old PHP for years, and getting really bored with it, getting started with a Framework can give you that crucial morale boost you have been lacking.
It’s like getting a new toy, and being able to build cool things with it. At least that’s how I felt when I first got started with Frameworks. I’m sure many of the web developers have gone through similar experiences.
Using a PHP Framework may or may not be the best choice for you. However, you should always keep an open mind and keep up to date with all the new developments in the web development world.
Frameworks are cool and hot today, and we can’t tell what tomorrow is going to bring. I guess all I’m saying is that they are definitely worth looking into.