Should You Use a PHP Framework? Five Pros and Cons

From routing HTTP requests to accessing the database and rendering the user interface, most web applications have a lot in common with each other. Many of them allow their visitors to sign in, and it's hard to imagine a web application without email sending. 

A software framework is a way to put this observation into use. 

By extracting the common traits and the application's underlying architecture and bundling them into a reusable template and collection of libraries, a framework allows developers to focus on what makes their project stand out.

In fact, when you use your previous project as a boilerplate instead of starting from scratch, you are already using an ad-hoc framework. The popular PHP frameworks—such as Laravel, Yii, or Symfony—take this approach to its logical conclusion by placing a strong focus on designing the framework's architecture and tooling and then making it available for other developers to use in their projects.

But is using a framework always the best way to go, or does it sometimes make sense just to start from scratch using nothing but plain PHP? 

To answer the question, in this tutorial, we will look at five pros and cons of using a PHP framework.

Five Pros of Using a PHP Framework

Let's start by looking at how using a PHP framework can help you in your application development.

1. Rapid Development

In today's startups based on experimentation, rapid prototyping, and getting your minimum viable product out in the world as fast as possible, development speed can be what makes or breaks your business.

Using a PHP framework is one way to answer this requirement.

A well-designed PHP framework selects the best implementations to common problems from the open-source community and, building on top of them, provides you with an application architecture (in most cases, based on the Model-View-Controller pattern) to guide your development.

This way, instead of spending time planning your application's architecture and evaluating the various libraries available, you get to start from a fully functional, if bare bones, template and get right to building the features specific to your application. Many PHP frameworks also come with a command-line tool that you can use to generate code and save more of your precious development time. 

2. A Framework Makes Your Application More Secure

While speeding up your development, a framework can also make you more secure: You are working fast, but that doesn't happen by hacking your way using quick and dirty solutions but �by using proven best practices and ready-made components.

The solutions included in a modern PHP framework are continuously tested and peer reviewed and validated by multiple developers using the framework. As the frameworks are open source, security issues and other �problems that you might overlook in your proprietary code get quickly noticed and fixed.

That said, it's important to remember that using a framework doesn't mean you can forget all about security and good development practices. Make sure you use the framework as it is meant to be used, keep it updated, and do your part in things such as parameter validation—using the frameworks' tools, it isn't all that difficult.

3. Easier Maintenance

By providing a sound structure for your application and nudging you towards software development best practices and patterns, a PHP framework guides you towards writing code that is loosely coupled and repeats itself as little as possible. In other words, it's easy to read, test, maintain, and develop further. 

Virtually all popular PHP frameworks today are object oriented and developed with automated testing and even test-driven development in mind.

You will still have to do your part. But compared to starting from scratch, a framework and its documentation reminding you of the concepts and nudging you towards using them will surely help.

Another way in which using a framework makes maintaining your application easier is through its shared components. As the framework libraries and helpers are developed independently of your application, you get to offload a big part of your application's maintenance to the framework developers.

In a way, it's like having a team of developers working for you, for free!

4. Stronger Teamwork

As a developer, you know your classes and functions by heart and can use them easily. But as you bring in team members, the situation changes. 

Without knowledge of your design decisions and complete documentation (let's face it: especially in the world of rapid development, documentation is often an afterthought), they will find it hard to wrap their heads around your application's code.

Using a PHP framework �will help make it easier for new developers to get started with working on the project. Chances are they are already familiar with the framework and can put all of their attention into understanding the specifics of the application. And even if the framework is new to them, the documentation, �online tutorials, and the familiar design patterns will help them get up to speed with minimal effort. 

All this while you can keep working on the application rather than teaching the developers your ways of thinking.

5. The Community Is There for You

Finally, a significant benefit of using a PHP framework comes from the active communities of developers and users that gather around them.

All major frameworks attract open-source contributors who, in addition to working on the framework itself, develop additional widgets, libraries, and helper components for others to include in their applications. �Other contributors write tutorials and books or create videos to explain the ins and outs of how they use the frameworks in real-life projects.

As a part of such a developer community, you will grow as a developer, learning quickly and applying what you learn to your application development.

And if you are so inclined, you can give back and participate in the open-source projects in any way that feels natural and fulfilling to you.

Five Cons of Using a PHP Framework

Now that we have seen how using a PHP framework can help you in your development, let's take a look at the downsides and why going without one might sometimes be the better option.

1. Slower Execution

Compared to running a simple PHP script, a PHP framework always adds complexity and overhead in the form of classes and libraries loaded before your code even gets called. The difference can be significant in quick scripts and APIs with a limited set of functionality that needs to run fast. 

In most web applications, however, the efficiency implications of a framework have more to do with how you use it than the framework itself. For example, while an Active Record implementation is a handy way of accessing the database, it doesn't make sense to use it in places where you have to work on multiple rows in the database at once. 

So this comes down to knowing your application's needs, but also not allowing yourself to be attached to a specific way of using it. Sometimes, even when working with a framework, it makes sense to sidestep and write some of the code at a lower abstraction level.

2. General Solutions for Specific Problems 

The idea of a PHP framework is to be a general solution for the recurring requirements most developers will face in developing a web application. But where should we draw the line in what to include in the framework and what to leave for the applications—or third-party add-ons—to implement?

According to some developers, this is where many frameworks go wrong. 

PHP's creator, Rasmus Lerdorf, has said: 

“Nobody has a general problem. Everyone has a very specific problem they’re trying to solve. And a general purpose framework, while it can solve it, usually solves it in a way that you get so many other things that you don’t need. That ends up being done on every request.”

Because of this, if you decide to use a framework, it's important to make sure you pick one that fits your application's needs. 

While most frameworks are based on lazy loading and won't include the parts of the framework that your application doesn't use, the added complexity will still be there, leading to unnecessary tinkering in removing functionality that came with the framework but that you don't need.

3. A Framework Takes Time to Learn and Master

While a PHP framework nudges you towards good code, it is no guarantee for it. If you work with a framework without understanding it and the design patterns behind it, you will most likely end up with a big, structured mess.

For someone who is already familiar with these design principles, from other frameworks or projects, the learning curve will be relatively short. For a developer with less experience, learning will take longer. Not to mention mastering the framework.

So, if you are in a hurry—and your project is small—it may not be the time to start learning a new framework. Instead, consider using a micro-framework or even starting from scratch and using some libraries you are familiar with. 

Then, take the time to learn a full-blown PHP framework when your schedule allows.

4. Limited Visibility and Control

Using a software framework moves your application development to a higher level of abstraction. This is by design, and usually a good thing. However, it's good to realize that it too can lead to problems.

While many parts of the framework's template are meant to be customized, the framework's core and its libraries are like a black box you use but don't have much control over. You may suggest improvements but ultimately, it's up to the framework's developers to decide whether they will include that change or not.

Also, if you start using a framework before fully understanding the PHP language itself, you may confuse the framework's features with language features—and will end up using the framework's functionality even when plain PHP would be a better choice.

5. A Framework Introduces an External Dependency

A framework is always an external dependency for your application—code controlled by someone other than you and your team. While this is usually not a problem, it's something worth considering if the software you are building is a core part of your business

Ask yourself what you will do if the framework’s development is dropped or moves in a direction you don’t want to follow. 

A good way to mitigate this risk is to use a reliable, popular framework with an active community of developers and users. But even then, it's something to give some thought to: are the benefits of using a framework big enough to make the risk worth taking?

Conclusion

A modern PHP framework is a powerful tool in a software developer's tool chest: it can save you a lot of time and effort and provide peace of mind as you develop your PHP application. Just remember that this shouldn’t replace learning core PHP first!

The key in the above conclusion, however, is selecting the right tool for the job. Every project is different, and even a framework that is perfect for one project might not be quite right for the next. Go for simplicity and ask yourself if a full-blown framework is right for the project, or if using a micro-framework such as Lumen could be a better solution. 

Starting from scratch using vanilla PHP is rarely the best option, but that too is worth considering when developing a very simple application (such as a website without much interactivity)—and remember that you can always extend it by using Composer to pick libraries of your choosing.

When selecting a framework, make sure the framework you choose is actively developed and supported and follows sound software engineering practices. This way, you're future-proofing your application by making sure the underlying framework will not suffer a quiet death.

And finally, even if you decide not to use one, it's a good idea to get familiar with one or more of the modern PHP frameworks. Studying them is an excellent way to learn and improve your craft in software development and help you stay on top of the evolving world of PHP development.

Additional Round-Ups

Source: Tuts Plus

About the Author