Home > Backend Development > PHP Tutorial > In-depth analysis of using PHP to implement MVC, brief analysis of PHP to implement mvc_PHP tutorial

In-depth analysis of using PHP to implement MVC, brief analysis of PHP to implement mvc_PHP tutorial

WBOY
Release: 2016-07-12 08:58:11
Original
806 people have browsed it

In-depth analysis of using PHP to implement MVC, and brief analysis of PHP to implement mvc

Using MVC in PHP is becoming more and more popular, especially in some open source frameworks. MVC is sufficient for most situations, but there are some situations where it is not suitable, such as relatively simple personal blogs. For blogs with only a few hundred articles, using MVC feels a bit too complicated; similarly for blogs with only a few hundred articles, MVC is not suitable for most situations. Portals such as Sina, using MVC, will have a large number of files loaded, and the impact on speed is unacceptable

The traditional face-to-face procedural development method begins to show its insufficiency when dealing with medium-sized and above applications. Even if we can complete the requirements quickly, we will fall deeply into the trap we built early after the requirements change or when performing post-maintenance. Therefore, using an object-oriented approach to implement the MVC pattern will provide us with a clear idea for sorting out the program's architecture.

What is MVC?

There are many definitions and explanations of MVC. We can find a more detailed explanation in Wiki or [2]. I do not intend or have the ability to explain it in depth here. From the perspective of PHP development, MVC can be summarized as:

The View:

When it comes to views, many of us think of template engines (such as Smarty, etc.). In fact, it is a variety of outputs, such as html templates and Javascript files.

Module (The Model)

Module represents the logic of the program and is often called the business logic layer in enterprise applications. Generally speaking, the work completed by this layer is to process the original data into meaningful data sequences stored according to the data structure we designed, and hand these data to the view for processing. Normally, a data abstract class is used in the module to perform processing related to data operations.

Model usually contains functions used to deal with the database.

The Controller

The controller is the first stop for all WEB applications. It accepts received parameters, such as the $_GET variable, and then responds accordingly.

There are also many debates about whether MVC is suitable for PHP. People continue to discuss whether MVC is suitable for PHP [3]. There are now many MVC frameworks, such as those listed in PHP MVC Frameworks [4]. So, why are people so keen on MVC, and why should we use MVC in our designs.

Why use MVC?

MVC was first used to solve desktop GUI programming problems. The earliest MVC framework should be Model 2 proposed by Sun in 1999, which later evolved into Struts. MVC gives people a deep impression, but when we use it, we don't seriously think about why we use MVC.

In traditional desktop applications, once something happens in the Model, we can actively refresh the View interface to show the changes that have occurred in the background. In web applications, we seem to be limited to the traditional Http Request/Response method, and we seem to have no way to let the client update. This discussion does not mean that MVC cannot be used to develop WEB applications, but that to some extent, it is not the most suitable.

There are still many debates about the use of MVC [1], but I believe that all people who are accustomed to using MVC to organize their own projects will definitely not give up MVC when asked to choose a new project architecture.

How to implement MVC?

The following is a super simple MVC structure implementation. Even the data source uses a built-in fixed array. Although simple, in fact, the ideas of many PHP Framework core implementations should be similar to this, but some frameworks It provides more tools that are convenient for developers to use. I also want to implement a PHP framework myself. I am currently planning it. I also hope that I can learn more PHP design ideas and methods from the development of the framework.

Controller.php

include 'Model.php';
include 'View.php';
class Controller {
private $model = '';
private $view = '';
public function Controller(){
$this->model = new Model();
$this->view = new View();
}
public function doAction( $method = 'defaultMethod', $params = array() ){
if( empty($method) ){
$this->defaultMethod();
}else if( method_exists($this, $method) ){
call_user_func(array($this, $method), $params);
}else{
$this->nonexisting_method();
}
}
public function link_page($name = ''){
$links = $this->model->getLinks();
$this->view->display($links);
$result = $this->model->getResult($name);
$this->view->display($result);
}
public function defaultMethod(){
$this->br();
echo "This is the default method. ";
}
public function nonexisting_method(){
$this->br();
echo "This is the noexisting method. ";
}
public function br(){
echo "";
}
}
$controller = new Controller();
$controller->doAction('link_page', 'b');
$controller->doAction();
==================================
Model.php
class Model {
private $database = array(
"a" => "hello world",
"b" => "ok well done",
"c" => "good bye",
);
//@TODO connect the database
//run the query and get the result
public function getResult($name){
if( empty($name) ){
return FALSE;
}
if( in_array($name, array_keys( $this->database ) ) ){
return $this->database[$name];
}
}
public function getLinks(){
$links = "Link A ";
$links.= "Link B ";
$links.= "Link C ";
return $links;
}
}
====================================================
View.php
class View {
public function display($output){
// ob_start();
echo $output;
}
}
Copy after login

Articles you may be interested in:

  • PHP implementation of MVC pattern
  • The simplest way to develop MVC in PHP - model
  • PHP MVC Analysis of the implementation of patterns in website architecture
  • Analysis of PHP's MVC pattern implementation principles (a simple MVC framework example)
  • PHP implementation of the simplest MVC framework example tutorial
  • PHP simply implements MVC
  • PHP implements simple MVC framework example
  • PHP simple MVC framework implementation method

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1105376.htmlTechArticleIn-depth analysis of using PHP to implement MVC, brief analysis of PHP to implement mvc Using MVC in PHP is becoming more and more popular. Especially among some open source frameworks. MVC is sufficient for most situations, but there are some situations...
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