Repositories, Adapters, MVC with all it cousins, SOLID, RTFM… As a (PHP) developer, those words are thrown at you from every corner of the web. And I hate it, I’ve had enough. Stop telling me what to do and show me those kittens instead.
Key Takeaways
- MVC is often touted as the best solution for web-application problems, but it does not inherently solve code complexity, code reuse, flexibility, or decoupling issues. These problems are solved by developers who write flexible, decoupled, and reusable code.
- MVC, like all patterns, is a tool that can help developers write flexible, decoupled, and easy-to-use code when used correctly. However, it has been heavily abused and misused in situations where it may not be the best fit.
- While patterns can be useful, they should not be used blindly or as a one-size-fits-all solution. Developers should understand the strengths and weaknesses of each pattern and use them appropriately to solve specific problems.
Software solves problems
We don’t just write software. Code doesn’t fall out of the sky into our files. We analyze the requirements, split them into small problems we know how to solve, and then we solve those small problems. Every line of code you write, have written and will ever write solves a certain problem. Be it to save the world, show kittens on a screen or make it look good in IE8. It’s there for a reason, don’t you touch it!
Problems are solvable and the solutions for these problems become part of something bigger. A black box that satisfies all initial requirements. But how do we solve these problems? Is my solution the best solution? Will other developers (or me in 2 months) understand what I did here?
One solution fits all
When solving the web-application problem, we are forced to use MVC. The dev community often frowns upon those who don’t use it, but seldom lets those they’re frowning upon ask why.
So why is MVC the best? Commonly regurgitated reasons are:
- Reduced Code complexity
- Code reuse
- Increased flexibility
- Decoupled code
Cool, sounds nice, but…
- Is it true?
- Do all other patterns lack these cool things?
The answer is no.
MVC doesn’t solve the Code complexity problem. It doesn’t solve the code reuse or no-flexibility problem either. And it doesn’t guarantee decoupled code.
Developers guarantee reduced code complexity. It’s us coders, programmers, developers and artists that write flexible, decoupled and reusable code. We need MVC as much as we need jQuery for document.getElementById(). We’ve been building great software before anyone had heard of MVC and we will continue building great stuff without MVC.
Don’t forget that MVC is a pattern, not a solution. It stands in line with all the others. Adapters, Factories, Singletons, Modules, Interpreters, Observers…
Patterns don’t solve problems, they help us
Patterns help us write flexible, decoupled and easy to use code. A pattern is a best practice that a developer can use to solve problems. Those best practices vary depending on the type of problem you are solving, there is no top 5. A boat might be really good at crossing water, but it can’t plough a field.
Each pattern has its strength and weaknesses and is tailored to tackle a certain situation. The Factory pattern for instance is really good at creating Objects. The Module pattern helps us write software modules in a language that doesn’t (or only partially) support modules (e.g. JavaScript). The Observer pattern’s strength is event handling and MVC helps us in decoupling layout, data manipulation and controllers.
But MVC is heavily abused, and here is why:
Somewhere down the road, someone decided that this was the best approach for everything written in PHP and accessible with a browser. Then came along the rule that a Model should have a 1-on-1 relation to a row in a database, Controllers should be thin and views have to be written in a templating language that compiles to PHP.
Then, someone noted that ‘thin controller’ is not always the best approach. They thus created the rule of fat controller, thin model.
Several iterations later, we made poor MVC give birth to HMVC, MVA, MVP, MVVM, PAC…
MVC is the new Singleton (or IE8)
Sadly enough, MVC isn’t the only abused pattern. As Keith nicely points out:
We needed something that looked like a global and acted like a global but wasn’t really a global.
Suddenly the Singleton pattern was showing up everywhere in our code. You could write crappy code without people complaining about the use of global $var
. Instead we used the Singleton pattern, Global::getInstance()->var
and called it OO.
Patterns are cool, developers aren’t
Don’t get me wrong, patterns are there to be used. But use them wisely. Use them together. There is nothing worse then a developer using the wrong pattern for the wrong reason with the wrong intentions.
So please, I beg you. Don’t abuse patterns.
Don’t reinvent the wheel. You are not the first developer trying to create modules in JavaScript, neither are you the first one developing event handling software. A lot of smarter, more trained professionals have done the exact same thing before you.
Having trouble separating your MySQL from your PHP? Are you writing SELECT * FROM
in your HTML? MVC will probably help you, or maybe Multitier architecture is a better fit for you.
Having troubles with lazy loading/reading config data? Singleton could help here.
Is creating an object of a certain class a pain in the ass? The factory pattern can really help you stay DRY.
Having troubles getting 2 services to communicate with each other? Adapters can help.
Conclusion
Depending on the situation, and the problem at hand, different patterns can help you write robust, secure and understandable code. Just be careful using them – if you catch yourself using the MVC pattern for a 1-pager, ctrl+a
del
.
May the patterns be with you!
Frequently Asked Questions about MVC and PHP Frameworks
What is the Model-View-Controller (MVC) pattern in PHP frameworks?
The Model-View-Controller (MVC) is a design pattern used in software engineering, particularly in web development using PHP frameworks. It separates an application into three interconnected components. The ‘Model’ corresponds to the data-related logic, the ‘View’ is responsible for the user interface and presentation of data, and the ‘Controller’ handles the communication between the Model and View. This separation allows for efficient code organization, easier debugging, and improved scalability.
Why should I use an MVC framework for my PHP project?
MVC frameworks provide a structured and efficient way to develop complex and scalable web applications. They offer benefits such as code reusability, separation of concerns, and easier maintenance. MVC frameworks also often come with built-in functionalities for database abstraction, form validation, session, and cookie handling, which can significantly speed up the development process.
What are some of the best MVC frameworks for PHP?
There are several MVC frameworks for PHP that are popular among developers due to their robust features and ease of use. Some of these include Laravel, Symfony, CodeIgniter, Yii, and Zend Framework. Each of these frameworks has its strengths and is suited to different types of projects, so the best one for you would depend on your specific needs and preferences.
How does Laravel compare to other PHP MVC frameworks?
Laravel is one of the most popular PHP MVC frameworks due to its elegant syntax, robust features, and vibrant community. It offers functionalities such as an ORM (Object-Relational Mapping) for database operations, a templating engine called Blade, and built-in support for user authentication and authorization. Compared to other frameworks, Laravel is often praised for its ease of use and comprehensive documentation, making it a great choice for both beginners and experienced developers.
What is a micro-framework and when should I use one?
A micro-framework is a minimalistic web application framework that provides only the essential features needed to build a web application. They are typically used for smaller projects or APIs where a full-featured MVC framework might be overkill. Some popular PHP micro-frameworks include Slim, Lumen (a micro-framework by Laravel), and Silex.
How can I choose the right PHP MVC framework for my project?
Choosing the right PHP MVC framework depends on several factors, including the size and complexity of your project, your team’s expertise, the framework’s community and support, and your personal preference. It’s recommended to research and try out a few different frameworks before making a decision.
Can I build a PHP application without using an MVC framework?
Yes, it’s entirely possible to build a PHP application without using an MVC framework. However, using a framework can provide structure, streamline the development process, and help prevent common security vulnerabilities. If you choose not to use a framework, you’ll need to handle these aspects yourself.
What are the learning curves for different PHP MVC frameworks?
The learning curve for a PHP MVC framework can vary depending on your familiarity with PHP and MVC concepts. Generally, frameworks like CodeIgniter and Laravel are considered beginner-friendly, while frameworks like Symfony and Zend may require a steeper learning curve due to their complexity and extensive features.
How does MVC help in developing scalable applications?
The MVC pattern promotes the separation of concerns, meaning different aspects of the application are decoupled from each other. This makes it easier to scale and maintain the application as it grows. For instance, you can modify the view without affecting the model, or vice versa. This separation also makes it easier to work in a team, as different developers can work on different components simultaneously.
Are there any drawbacks to using MVC frameworks?
While MVC frameworks offer many benefits, they also have potential drawbacks. These can include a steeper learning curve, especially for beginners, and potential performance overhead due to the additional abstraction layers. However, these drawbacks are often outweighed by the benefits for larger, complex projects. For smaller projects, a micro-framework or even plain PHP might be more suitable.
Jeroen Meeus is a developer that knows how to appreciate his glass of CH3CH2OH with well written code to read on a lazy sunday. When frustrated, he tends to share those frustrations at his so-called blog.