Home > Article > Backend Development > MVC pattern application skills sample code in php
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
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); }
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(); }
Interface Controller { public function pop(); public function push(); public function execute(); } Interface View { public function display(); }
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; } }
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); }
class Tech extends DataBaseOperation { public function select($info) { //在这里实现你的代码 } public function selectNum($info) { //在这里实现你的代码 } }
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新的特性,异常的处理 } }
abstract ArticleView implements View { protected $smarty = null; public function construct() { $this->smarty = new Smarty(); ///下面你可以定义smarty的一些属性值 } }
class TechArticleView extends ArticleView { public function display() { //实现你的代码,调用Tech类和更多的DataBaseOperation子类 } }
try { $viewController = new ViewController(); $viewController->push("tech",TechArticleView); //持续的增加 $mod = $_GET["mod"]:$_GET["mod"]:$_POST["mod"]; //最后 $viewController->execute($key); } catch( Exception $e) { //如何处理异常就是你的事了 }
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!