Home > Backend Development > PHP Tutorial > MVC pattern application skills sample code in php

MVC pattern application skills sample code in php

怪我咯
Release: 2023-03-13 17:58:01
Original
1404 people have browsed it

MVC pattern is the abbreviation of "Model-View-Controller", and the Chinese translation is "Model-View-Controller". MVC applications always consist of these three parts. Event (event) causes the Controller to change the Model or View, or both at the same time. As long as the Controller changes the data or properties of the Models, all dependent Views will be automatically updated. Similarly, whenever the Controller changes the View, the View will refresh itself by getting data from the underlying Model. The MVC pattern was first proposed by the Smalltalk language research group and is used in user interaction applications. There are many similarities between the smalltalk language and the java language. They are both object-oriented languages. Naturally, SUN recommended the MVC pattern as the architectural pattern for developing Web applications in the petstore (pet store) example application. The MVC pattern is an architectural pattern that actually requires the collaboration of other patterns. In the J2EE mode directory, service to worker mode is usually implemented, and service to worker mode can be composed of centralized controller mode, dispatcher mode and Page Helper mode. Struts only implements the View and Controller parts of MVC. The Model part needs to be implemented by developers themselves. Struts provides the abstract class Action so that developers can apply Model to the Struts framework. Model is the part that represents component status and low-level behavior. , it manages its own state and handles all operations on the state. The model itself does not know who is using its own view and controller. The system maintains the relationship between it and the view. When the model changes, the system is also responsible for notifying The corresponding view. View represents a visual representation of the data contained in the management model. A Model can have more than one View, but this is rarely the case in Swing.

The Controller manages the control of the interaction between the model and the user. It provides some methods to handle situations when the model's state changes.

Using the MVC pattern in php

First of all, let me give an example:

A simple
Article display
SystemIn a simple period, we assume that this article system is only Reading, which means that this example will not involve the publication of the article, starts now. Since it only involves database reading, I defined two interfaces

Interface DataOperation 
{ 
   public function select($info); 
   public function selectNum($info); 
}
Copy after login

The above interface defines the interface for reading data,

select method

will return all Articles needed. The selectNum method returns the total number of articles, which is used for paging display. $info is an array used to store query conditions

Interface DataSource 
{ 
   public static function getInstance(); 
}
Copy after login

Here we assume that we are operating a database, DataSource defines an interface, and all instance classes that implement this interface will get a static object

Interface Controller 
{ 
   public function pop(); 
   public function push(); 
   public function execute(); 
} 
Interface View 
{ 
   public function display(); 
}
Copy after login

Okay, let's implement it.

The following defines a class to implement the DataSource interface. This class uses the

single case mode

class DataBaseSource implements DataSource 
{ 
   public static $instance = null; 
   public static function getInstance() 
   { 
       if(self::$instance == null) 
       { 
           self::$instance == new PDO("mysql:host=localhost;dbname=article","root","123456"); 
       } 
       return self::$instance; 
   } 
}
Copy after login

Definition An

Abstract class

to implement DataOperation, we need to share a database connection, so I initialize the database object in the abstract class, so that all subclasses can share this object

abstract class DataBaseOperation implements DataOperation 
{ 
   protected $db = null;  
   public function construct() 
   { 
       $this->db = DataBaseSource::getInstance(); 
   } 
   public function select($info); 
}
Copy after login

Next I will write a business subclass to implement the abstract class DataBaseOperation


class Tech extends DataBaseOperation 
{ 
   public function select($info) 
   { 
       //在这里实现你的代码 
   } 
   public function selectNum($info) 
   { 
       //在这里实现你的代码 
   } 
}
Copy after login

We have implemented the business logic layer, and the following is the control layer

class ViewController implements Controller 
{ 
   private $mod = array(); 
   public function push($key,$value); 
   { 
       //实现你的代码,将类注册进$this->mod; 
   } 
   public function pop($key) 
   {         
       //实现你的代码,将$this->mod[$key]值为null; 
   } 
   public function execute($key) 
   { 
       //在这里实现你的代码,生成实例.注意利用php5新的特性,异常的处理 
   } 
}
Copy after login

Okay, here is the presentation layer, where the Interface View

abstract ArticleView implements View 
{ 
   protected $smarty = null; 
   public function construct() 
   { 
       $this->smarty = new Smarty(); 
       ///下面你可以定义smarty的一些属性值 
   } 
}
Copy after login

will be implemented. Specific pages, such as the display page of scientific and technological articles

class TechArticleView extends ArticleView 
{ 
   public function display() 
   { 
       //实现你的代码,调用Tech类和更多的DataBaseOperation子类 
   } 
}
Copy after login

Okay, here is the general entrance. index.php

try 
{ 
   $viewController = new ViewController(); 
   $viewController->push("tech",TechArticleView);   
//持续的增加   
   $mod = $_GET["mod"]:$_GET["mod"]:$_POST["mod"]; 
   //最后 
   $viewController->execute($key); 
} 
catch(
Exception
 $e) 
{ 
       //如何处理异常就是你的事了 
}
Copy after login

The above is the detailed content of MVC pattern application skills sample code in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template