Introduction to MVC in PHP

Introduction to MVC in PHP

MVC stands for Model View Controller. It’s a software architecture which keeps the separate presentation of data with the actual method which interacts with the data. Previously we have learned the OOP concepts and class example in PHP. In this tutorial, we will learn Model View Controller Approach, MVC in PHP.


For example, we have our data in the database, so the interaction with that data is the job of model and view is used to show that data to the user and the controller control the user commands and update that command in the model.

The MVC pattern allows both front-end and back-end developers to work on the same system without knowledge of each other work. It allows both to work separately but parallel.



A model holds the data i.e. the variables and the members. The model becomes the bridge between view and controller. The model should be accessed by the controller to be viewed and written. Controller should also be able to delete data in the Model.


The view is the user interface or the graphical layout i.e. HTML of the project where data from Model is displayed. It is the final output of the processed data. A very simple example of the view is a simple text field, Controller process the data from the text field and then the data is stored in the model.


Controller is the main component in MVC. It processes data from the user and interacts with the Model to update the view.


To illustrate the work on MVC pattern we take a simple example.

In this example we hard coded username and password in model class and using view we show them to user and index.php file is used to access the returning values of view and then if user wants to update the data again we hard code the new username and password so that if user clicked on the update option the controlled class active, and it requests model with new data and then updates data in it.

Model Class:


Controller Class:

View Class:

Index File: