The traditional development method of physiognomy process is inadequate when dealing with medium and above applications. Even if we were able to complete the requirements quickly, we would fall deeply into the traps we had set up earlier when the requirements were changed or later maintained. Therefore, using object-oriented approach to implement MVC pattern will provide us with a clear idea to comb through the architecture of the program. What is MVC? There are many different definitions and interpretations of MVC. A more detailed explanation can be found in Wiki or [2], but I’m not going to, and I’m not going to be able to go into it. From a PHP development perspective, MVC can be summarized as: The View: When we think of views, many of us think of template engines (Smarty, etc.). This is a variety of output, such as HTML templates and Javascript files. The Model represents The logic of The program and is often referred to as The business logic layer in enterprise applications. In general, what this layer does is process the raw data into a meaningful sequence of data stored in our designed data structure, and hand that data to the view to process. Typically, a data abstraction class is used in a module for processing related to data manipulation. The Model usually contains the functions used to interact with the database. The Controller is The first stop of any WEB application. It accepts The parameters it receives, such as The $_GET variable, and then reacts accordingly. There are also many arguments about whether MVC is suitable for PHP. People constantly discuss whether MVC is suitable for PHP[3], and there are many MVC Frameworks, such as those listed in PHP MVC Frameworks [4]. So, why are people so passionate about MVC, why do we use MVC in our design? Why MVC? MVC was originally used to solve the programming problems of desktop GUI. The earliest MVC framework is probably Model 2 proposed by Sun in 1999 and later evolved into Struts. MVC is impressive, but we don’t really think about why we use MVC in the process of using it. In a traditional desktop application, we can actively refresh the View to show changes in the background as soon as there is time to happen in the Model. In Web applications, we seem to be limited to the traditional Http Request/Response mode, and we seem to have no way to get the client to update. This discussion is not to say that MVC cannot be used for WEB application development, but to some extent, it is not the most appropriate. There is much debate [1] about the use of MVC, but I believe that anyone who is used to using MVC to organize their projects will not abandon MVC when choosing a new project architecture. How do YOU implement MVC? The following is a super simple implementation of the MVC structure. Even the data source uses a built-in fixed array. Although simple, the idea of many PHP Framework core implementations is similar to this, but some frameworks provide more convenient tools for developers. I also want to implement a PHP framework by myself, and I am planning it now. I also hope I can learn more PHP design ideas and methods from the framework development. 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 “<br />”;

}

}





$controller = new Controller();

$controller->doAction(‘link_page’, ‘b’);

$controller->doAction();




Model.php

\

Code\

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; }}

Resources: 1, the MVC and Web apps www.sitepoint.com/blogs/2005/… 3, 2, the Model View Controller http://www.phpwact.org/pattern/model_view_controller Is MVC over designed Crap Phplens.com/phpeverywhe… 4, PHP MVC Framworks http://www.phpwact.org/php/mvc_frameworks 5, Model 2 en.wikipedia.org/wiki/Model_… 6, Stupid Easy MVC http://codesnipers.com/?q=node/158&&title=Stupidly-Easy-MVC-in-PHP-or-