MVC Architecture

MVC is a software architectural pattern for implementing user interfaces on computers. It divides a given application into three interconnected parts. This is done to separate internal representations of information from the ways information is presented to, and accepted from the user.

  • MVC stands for “Model view And Controller“.
  • The main aim of MVC Architecture is to separate the Business logic & Application data from the USER interface.
  • Different types of Architectures are available. These are 3-tier Architecture, N-tier Architecture, MVC Architecture, etc.
  • The main advantage of Architecture is Reusability, Security and Increasing the performance of Application.
PHP MVC Architecture

Model: Database operation such as fetch data or update data etc.

View: End-user GUI through which user can interact with system, i.e., HTML, CSS

Controller: Contain Business logic and provide a link between model and view.

Let’s understand this MVC concept in detail:

Model:

  • The Model object knows all about all the data that need to be displayed.
  • The Model represents the application data and business rules that govern to an update of data.
  • Model is not aware about the presentation of data and How the data will be display to the browser.

View:

  • The View represents the presentation of the application.
  • View object refers to the model remains same if there are any modifications in the Business logic.
  • In other words, we can say that it is the responsibility of view to maintain consistency in its presentation and the model changes.

Controller:

  • Whenever the user sends a request for something, it always goes through Controller.
  • A controller is responsible for intercepting the request from view and passes to the model for appropriate action.
  • After the action has been taken on the data, the controller is responsible for directly passes the appropriate view to the user.
  • In graphical user interfaces, controller and view work very closely together.

Interface

  • An interface is similar to a class except that it cannot contain code.
  • An interface can define method names and arguments, but not the contents of the methods.
  • Any classes implementing an interface must implement all methods defined by the interface.
  • A class can implement multiple interfaces.
  • An interface is declared using the “interface” keyword.
  • Interfaces can’t maintain Non-abstract methods.

Example 1

<?php  
    interface a  
    {  
        public function dis1();  
    }  
    interface b  
    {  
        public function dis2();  
    }  
  
class demo implements a,b  
{  
    public function dis1()  
    {  
        echo "method 1...";  
    }  
    public function dis2()  
    {  
        echo "method2...";  
    }  
}  
$obj= new demo();  
$obj->dis1();  
$obj->dis2();  
  
?>  

Output:

method 1…method 2…

Example 2

  

<?php  
    interface i1  
    {  
        public function fun1();  
    }  
    interface i2  
    {  
        public function fun2();  
    }  
class cls1 implements i1,i2  
{  
    function fun1()  
    {  
        echo "protutorials";  
    }  
    function fun2()  
    {  
        echo "tech";  
    }  
}  
$obj= new cls1();  
$obj->fun1();  
$obj->fun2();  
  
?>

Output:

protutorialstech

Inheritance

It is a concept of accessing the features of one class from another class. If we inherit the class features into another class, we can access both class properties. We can extends the features of a class by using ‘extends’ keyword.

  • It supports the concept of hierarchical classification.
  • Inheritance has three types, single, multiple and multilevel Inheritance.
  • PHP supports only single inheritance, where only one class can be derived from single parent class.
  • We can simulate multiple inheritance by using interfaces.

Example 1

<?php  
class a  
    {  
        function fun1()  
        {  
            echo "protutorials";  
        }  
    }  
    class b extends a  
    {  
        function fun2()  
        {  
            echo "SSSIT";  
        }  
    }  
    $obj= new b();  
    $obj->fun1();  
?>  

Output:

protutorials

Example 2

<?php  
    class demo  
    {  
        public function display()  
        {  
            echo "example of inheritance  ";  
        }     
    }  
    class demo1 extends demo  
    {  
        public function view()  
        {  
            echo "in php";  
        }     
    }  
    $obj= new demo1();  
    $obj->display();  
    $obj->view();  
?>

Output:

example of inheritance in php