PHP - Why do we need to use a Framework?
As may you all know, PHP is one of the most popular server-side scripting languages in the world, and it has advanced a lot since its release
Need for a Framework?
As may you all know, PHP is one of the most popular server-side scripting languages in the world, and it has advanced a lot since its release.
In this era, where technology have moved beyond the basics. Developers need to build complex websites and web applications, and above a certain complexity level. It can take too much time and energy to always start from the scratch, hence developers felt the need of some boilerplate to start their development with. Therefore, several frameworks came into being to provide most of the adequate solutions to complex problems.
In this post, we’d look into the most basic benefits & advantages we get while using the frameworks, instead of writing code the procedural way. So, let’s dive straight in.
Code & File Organization
Just because you created an “includes” or “libs” folder & made a “functions.php” file, doesn’t mean your code is organized.
But when you setup a PHP Framework, it already contains a certain folder structure. Once you get used to this model, you’ll never want to go back to the procedural world.
Unfortunately, for some CLI champs, who still use VI, this could be a little tricky because they need to work with more files & directories. On the other hand, when you use a decent modern Code Editor or some IDE, it’ll be like a breeze to browse through your application code base and find what you need and yes quickly.
Utilities & Libraries
As we all know, PHP is a great language for web development & provides countless number of tools and libraries. However, if you ever built a website using PHP without a framework, you might found yourself hunting down a lot of third party code and libraries, or might have tried to write them yourself.
But with any PHP Framework, they’re packed with certain Helpers & Libraries that could help you with Form Validation, I/O Filtering, Database Abstraction, Session & Cookie Handling, Email Management, Calendar, Pagination and so on. Not to mention, there are several third party Plugins provided by the community that you can add to your framework manually or using dependency manager.
The MVC Pattern
As PHP itself works like a template engine, however, when used irresponsibly, it leads to very ugly and UN-maintainable code. MVC(Model View Controller) comes to the rescue here, the way MVC Pattern applies to PHP Applications:
Models represents your data structure, usually by interfacing with the Database.
View contains page templates and output data.
Controller handles the HTTP requests between the user and the server and binds everything together.
This kind of code separation leads to a more cleaner, readable and more maintainable structure of the application.
Security is one of the major issues to resolve for developers. In PHP, you may find several I/O filtering functions to protect your application against certain attacks. However, manually using these functions can get a little tiring job to do & you may forget about them in every once in a while.
Using some framework gets you cover here as well, they provide most of the solutions to your applications security out of the box, which is really helpful.
Let’s take Laravel for a example here,
It uses Hash and Bcrypt functions for encryption and to store Sensitive information
The built-in Auth system helps to decide user boundaries within the application.
It provides an easy method to protect your application from cross site request forgeries
It offers Database and Eloquent Authentication drivers out of the box.
It provides a basic HTTP authentication to authenticate users of your application without setting up a dedicated login page
Blade Template engine helps prevent any XSS attacks.
and the list goes on and on.
Less Code & Faster Development
There is of course a learning curve for all PHP Frameworks, but once you get over this knob, you’ll enjoy the awesomeness of Rapid Application Development. You’ll write lesser code, which means less time spent typing. You’ll not have to chase down third party libraries all the time for every single project as most of them already includes in the framework.
Also, since you’re being more organized, it’ll be much faster to chase down bugs, maintain code & make changes to existing code.
All Popular PHP Frameworks have great active communities behind them. You can talk to other developers, get help, feedback and could also give something back to the community.
One of the main arguments from the negative audience comes in this subject. There is obviously a performance hit when you build a simple “hello world” application with a framework versus plain PHP code.
But those benchmarks are just bad examples. First of all, you should understand that Developers are more expensive than the Servers.
Saving time from development & maintenance is likely to over weigh 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 will help you do Caching, Benchmarks, profiling etc. Moreover, Modern Frameworks have also great support for Dynamic Loading as they load code whenever needed
And It’s FUN
This might actually be the most important point of all, when you’ve fun doing your work, you’ll be more productive and happier in general.
So, if you’ve been using plain old PHP for years & getting really bored with it. Getting started with a framework could give you that crucial morale boost that you might lack. It’s like getting a new life. A lot of toys to play with and to build cool things with it. At least that’s how I felt when I’ve moved to frameworks & I’m sure many of the Web Developers have gone through similar Experiences.
In my next Article, I’ll discuss the differences, benefits and drawbacks of some of the Top PHP Frameworks, so stay Connected.
If you’ve any queries, please feel free to write it down in the comments or you can contact me on Twitter.