Convert Object Oriented approach to MVC pattern in PHP

0
492

Download Source Code

Convert a simple database application that is written through Object Oriented Approach to MVC pattern in PHP

Introduction

Model-View-Controller (MVC) is widely used by web developing due to its emphasis on separation of concerns, and thus indirectly, reusable code. The pattern encourages the development of modular systems, allowing developers to quickly update, add, or even remove functionality. In theory, a well-developed MVC system should allow a front-end developer and a back-end developer to work on the same system without interfering, sharing, or editing files either party is working on.

In this article, I will go the basic principles of MVC, a run through the definition of the pattern and a quick example of MVC in PHP that will be split format of the basic PHP coding. This is definitely read anyone who has never coding with MVC before or those wanting to brush up on previous MVC development skills.

What is MVC?

MVC is an Architectural pattern that describes a way to structure our application and the responsibilities and interactions for each part in that structure. MVC is popular as it isolates the application logic from the user interface layer and supports separation of concerns. Here the Controller receives all requests for the application and then works with the Model to prepare any data needed by the View. The View then uses the data prepared by the Controller to generate a final presentable response. Apart from isolating the view from the business logic, the M-V-C separation reduces the complexity when designing large applications.

It was first described in 1979 and, obviously, the context was a little bit different. The wild popularization of the MVC for web applications is due to its inclusion in two development frameworks that have become immensely popular: Struts and Ruby on Rails.

Action of MVC/Workflow of MVC

MVC is design framework which decreases the coupling between the objects by separating business object (Model), user interface (View) and business logic (Controller). A visual representation of a complete and correct MVC pattern and their interaction is looks like the following diagram:

Diagram of MVC Pattern in PHP

The model is responsible to manage the data; it stores and retrieves entities used by an application, usually from a database, and contains the logic implemented by the application.

The view (presentation) is responsible to display the data provided by the model in a specific format. It has a similar usage with the template modules present in some popular web applications, like WordPress, joomla …

The controller handles the model and view layers to work together. The controller receives a request from the client, invokes the model to perform the requested operations and sends the data to the View. The view formats the data to be presented to the user, in a web application as an html output.

Sample Database Application using OOP

Here I am present a sample database application using PHP Object Oriented Programming. Here I have two class that first one is Model that is used to connect the database and fetch the data from a table for the selected column and another class that will display the data that was fetched by the previous class. The complete code segment for the full process is as following,

Before run the code please make sure that server-name, username, password, database is correct in the connection string. And here I have used the table name student that will accept three columns (Roll, Name, Class, and Gender)

Convert OOP Application to the MVC pattern

It is possible to write above database application whose architecture is based on the MVC pattern. Let’s start with a bare bones example:

Following is the file structure to implement the whole application based on MVC pattern.

Model Interface

The model is responsible for managing the data of the application. It responds to the request from the view and it also responds to instructions from the controller to update itself.

Here I have design a class “Model” that will be established the connection with the database and a method of that class will fetch the data from the table.

The Controller

The controller is responsible for responding to user input and perform interactions on the data model objects. The controller receives the input, it validates the input and then performs the business operation that modifies the state of the data model.

In this project, I have design the controller by a class “Controller”. I have create an object to call the “Model” class within the constructor function and another method “invoke” within the class “Controller”, call the method “fatch” from the class “Model” and that data is displayed thought the View file “studentlist.php”.

View Presentation

A presentation of data in a particular format, triggered by a controller’s decision to present the data. It is the data representation layer. Here we have two file index.php and studentlist.php within the View layer. In the index.php function call the “invoke” method though the controller object. And another file “studentlist.php” is used to display the list of data that was fetch previously.

Conclusion

This is very simple example for creating MVC application in PHP from a Basic Object Oriented Programming. We’ve covered the basic theory behind the MVC pattern and have produced a very basic MVC application, but still we have a long way to go before implementing real life project. We have also seen the advantages of using an MVC framework that provides us with a basic MVC skeleton and few functionality, improving our productivity and making the development process easier. So Have a Happy Coding to you … 🙂

LEAVE A REPLY