PHP and MVC Design Pattern

The root cause of writing Software is simple Software solves problems. We analyze the requirements and break them into smaller problems (modules) and then write the solution for each such problem. Every line of code (LOC) we write is meant to resolve some problem. A pattern or design pattern defines how we solve problems, we must remember that a pattern is never the solution itself it is a way solution is written or developed or rather architected.

Around the world developers and solution architects are always excited about using MVC in fact the developer community often frowns upon those who don’t use it! I remember one of friends who once asked me… “OMG! You are not using any MVC of framework? Just writing native code?”… I tried hard to reason… “We may not be using MVC but we are using patterns and its pure OOP, I don’t think such application really requires MVC!”… “No… No… I think you must!”… I resigned!

What exactly MVC has to offer:

  • Reduced Code complexity
  • Code reusability
  • Increased flexibility
  • Decoupled code

Have we ever thought? MVC cannot solve the Code complexity or reusability or even flexibility problem, it’s the developers who can solve these problems. Writing decoupled code is also a developer’s thing; a pattern like MVC will create and environment with certain restrictions and rules but the code has to be written by a developer, the logic behind the code has to be thought out by a human!

Before I begin talking in favor of MVC let me clarify that MVC is a pattern, not a solution. It stands in line with all the other patterns like Adapters, Factories, Singletons, Modules, Interpreters, Observers… and so on.

What is MVC?

MVC is much more than a three-letter acronym, which stands for Model-View-Controller; it has become something of a standard in modern web application designs because most of the web applications can be split into the same three tiers MVC has to offer; presentation (View), business logic (Controller) and data access (Model). At the heart of MVC lies the concept separation of concerns or separated presentation, which creates a separation between the domain objects and the GUI elements (user interface).

  • Model – It constructs the central component of the MVC pattern, application attributes and behaviors are contained in the Model, and hence they hold the business logic of the application.
  • Controller – It controls the application interactions; it sends commands to the Model to update Model’s state and it can also send commands to the View depending upon Model’s response.
  • View – It is the user interface or GUI, which is presented to the end user who is completely unaware of the back end operations or any application. It collects data from the end user and sends to the Controller for processing and takes back processed data from the Controller to represent it back to the end user.

History of MVC

The concept of MVC was originally invented by Smalltalk programmers. It was invented by one Smalltalk programmer, Trygve Reenskaug. Trygve maintains a page that explains the history of MVC in his own words. He arrives at those following definitions in a paper he published on December 10th, 1979:

Models

Models represent knowledge. A model could be a single object (rather uninteresting), or it could be some structure of objects.

There should be a one-to-one correspondence between the model and its parts on the one hand, and the represented world as perceived by the owner of the model on the other hand.

Views

A view is a (visual) representation of its model. It would ordinarily highlight certain attributes of the model and suppress others. It is thus acting as a presentation filter.

A view is attached to its model (or model part) and gets the data necessary for the presentation from the model by asking questions. It may also update the model by sending appropriate messages. All these questions and messages have to be in the terminology of the model, the view will therefore have to know the semantics of the attributes of the model it represents.

Controllers

A controller is the link between a user and the system. It provides the user with input by arranging for relevant views to present themselves in appropriate places on the screen. It provides means for user output by presenting the user with menus or other means of giving commands and data. The controller receives such user output, translates it into the appropriate messages and pass these messages on to one or more of the views.

PHP and MVC

It is possible to write a web application in PHP without using any readily available MVC framework whose architecture is based on the MVC pattern where the entire application architecture and libraries needs to be written from scratch following the MVC principles. However there are various frameworks available for PHP, which use MVC principles and using them would remove the time and effort required to build the generic MVC code and hence increase scalability and save cost.

While choosing a framework, architects and programmers must consider the following factors:

  • Removes as much of the redundant coding as possible
  • Implements separation of concerns
  • Ensures good coding practice
  • Gives as much flexibility as possible to the coder

Below I have listed a few popular PHP MVC Frameworks that can be used to build Agile Scalable Robust Web Applications. You have to choose one or more based on the need of your application.

  1. Zend Framework 2
  2. Zend Framework
  3. Symphony
  4. CakePHP
  5. CodeIngiter
  6. Laravel
  7. Phalcon
  8. Prado
  9. Aura
  10. Fat-free
  11. PHP-MVC
  12. Fuel-PHP
  13. Kohana
  14. Slim
  15. Yii