Join Drew Falkman for an in-depth discussion in this video Why use a framework?, part of MVC Frameworks for Building PHP Web Applications.
- View Offline
- As you likely already know, PHP is a solid application development platform. It provides services for handling email, database interaction, and more. Because of its power and flexibility, it's easy to get in there and start coding your application without using any framework. So, what is a framework, and why should we use one? It's a very good question. Let's have a look. Coding in straight PHP works, but if you're not careful, there are a number of things that can happen. For starters, we can get what we call spaghetti code.
Spaghetti code is exactly what it sounds like, meandering code that does a lot of different things in one single place. When we don't have our code organized, we can end up doing redundant changes because we end up having to copy and paste things to get them to work, or our code is embedded in long files, and in order to make changes, we have to change them in multiple places. Disorganization is a big problem with writing straight PHP code. You can put things wherever you want, and because of that, things can get very confusing.
You can't as easily reuse pieces of your code throughout a regular PHP application. One of the big problems of not using a framework is that whenever you go to make a change, you can easily break other items within your code. Testing is another problem that arises when using straight PHP code. Because things aren't broken up into fine units, it can be extremely difficult to test different aspects of your code. When you're using straight PHP, you often end up writing things from scratch. Many of the frameworks come with some code built in that can help you to prevent having to do this.
When you're working in a team, in a regular PHP application, it can be very difficult to have multiple people working on different aspects of the application at the same time. Frameworks provides solutions for many of these problems. The main thing that frameworks provide is a single recipe that we use for developing our applications. We break things up into nice, small pieces so that it's easy to reuse our code throughout different aspects of our application. Having these small pieces also means that if I need to change an aspect of my code, I can change it in the one piece that's being reused throughout the entire application.
Makes it much easier to make changes in the future. Using this recipe allows us to have a nice, clean organization to our code. If I want to go in and change how one particular screen works or functions, it's easy for me to find where that code is based on the recipe I've used from the framework. One of the large advantages of all the frameworks we'll discuss in this course is that there are components built in to the framework that we can use to do all kinds of functionality. This can be from very small, low-level aspects, like connecting to a database, all the way to large aspects of our code, like handling authentication.
Because our code is broken into these nice, small pieces, it's very easy to have a team working on the application. One of the key advantages to using frameworks is to allow multiple people on a team to access different aspects of the application at the same time. So, do you always need to use a framework? One thing to consider is that using a framework is going to take more steps. Whereas in a single PHP page I can have all of my logic, my database connection, and my view connected.
In a framework, I have to break these all up into different components, and that can take additional time. Frameworks do come with a learning curve. We have to learn how the code is organized. We have to learn the API and what's available inside of the framework. There isn't always a benefit to overorganizing your code. In general, smaller applications that require one or two pages, you might be able to get away with not using a framework. The end result is that in an application using a framework, we have our code broken up in a fashion that's more organized, easier to reuse, and prevents us from redundant programming.
We get a head start if we're using components and services that are included in the framework. It can also be easier for teams negotiating to work on overlapping functionality because of the more granular pieces of this application. Ultimately, frameworks lead to better organization of code, which in most cases can make our application development experience less painful.
- Why use a framework?
- Introducing MVC-framework concepts
- Examining each framework's components
- Setting up the software
- Walking through sample apps built in each framework
- Comparing frameworks