用最简洁的代码构建MVC分层

首先来看MVC的关系图:



我共要创建五个文件:
index.php
DataAccess.php            数据库类
ProductModel.php        模型的实现
ProductController.php  控制器的实现
ProductView.php          视图的实现


index.php
  1. <?php
  2. require_once('lib/DataAccess.php');
  3. require_once('lib/ProductModel.php');
  4. require_once('lib/ProductView.php');
  5. require_once('lib/ProductController.php');
  6. $dao = new DataAccess ('localhost','user','pass','dbname');
  7. $productModel = new ProductModel($dao);
  8. $productController = new ProductController($productModel,$_GET);
  9. echo $productController->display();
  10. ?>
复制代码
首先我们包含所有文件, 创建数据库对象,然后用他作为参数创建模型对象,然后用模型对象创建控制器对象,再调用控制器的方法输出打印,这里控制器继承了视图类.


DataAccess.php 

  1. <?php
  2. class DataAccess {
  3.     private $db;
  4.     private $query; 
  5.     public function DataAccess ($host,$user,$pass,$db) {
  6.         $this->db=mysql_pconnect($host,$user,$pass);
  7.         mysql_select_db($db,$this->db);
  8.     }
  9.     public function fetch($sql) {
  10.         $this->query=mysql_unbuffered_query($sql,$this->db); 
  11.     }
  12.     public function getRow () {
  13.         if ( $row=mysql_fetch_array($this->query,MYSQL_ASSOC) )
  14.             return $row;
  15.         else
  16.             return false;
  17.     }
  18. }
  19. ?>
复制代码

这是一个简单的数据库类.很容易理解.


模型 -- ProductModel.php
  1. <?php
  2. class ProductModel {
  3.     private $dao;
  4.     public function ProductModel ($dao) {
  5.         $this->dao = $dao;
  6.     }
  7.     public function listProducts($start=1, $rows=50) {
  8.         $this->dao->fetch("SELECT * FROM products LIMIT ".$start.", ".$rows);
  9.     }
  10.     public function listProduct($id) {
  11.         $this->dao->fetch("SELECT * FROM products WHERE PRODUCTID='".$id."'");
  12.     }
  13.     public function getProduct() {
  14.         if ( $product=$this->dao->getRow() )
  15.             return $product;
  16.         else
  17.             return false;
  18.     }
  19. }
  20. ?>
复制代码

模型中已经封装了获取数据的不同方法,这正是他的职责所在:企业数据和业务规则. 上海性病医院 模型拥有最多的处理任务.

视图 -- ProductView.php

  1. <?php
  2. class ProductView {
  3.     private $model;
  4.     private $output;
  5.     public function ProductView ($model) {
  6.         $this->model= $model;
  7.     }
  8.     public function header () {
  9.         $this->output=<<<EOD
  10. <!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
  11. <html>
  12. <head>
  13. <title> Our Products </title>
  14. <style>
  15. body { font-size: 13.75px; font-family: verdana }
  16. td { font-size: 13.75px; font-family: verdana }
  17. .title { font-size: 15.75px; font-weight: bold; font-family: verdana }
  18. .heading {
  19.     font-size: 13.75px; font-weight: bold;
  20.     font-family: verdana; ">
  21. .nav { ">
  22. </style>
  23. </head>
  24. <body>
  25. <div align="center" class="title">Our Products</div>
  26. EOD;
  27.         $this->output.=" <div align="right"><a href="".
  28.             $_SERVER['PHP_SELF']."">Start Over</a></div> ";
  29.     }
  30.     public function footer () {
  31.         $this->output.="</body> </html>";
  32.     }
  33.     public function productItem($id=1) {
  34.         $this->model->listProduct($id);
  35.         while ( $product=$this->model->getProduct() ) {
  36.             $this->output.="<p><b>Name</b>:".$product['PRODUCTNAME']."</p>".
  37.                 "<p><b>Price</b>:".$product['UNITPRICE']."</p>".
  38.                 "<p><b># In Stock</b>:".$product['UNITSINSTOCK']."</p>";
  39.             if ( $this->$product['DISCONTINUED']==1 ) {
  40.                 $this->output.="<p>This product has been discontinued.</p>";
  41.             }
  42.         }
  43.     }
  44.     public function productTable($rownum=1) {
  45.         $rowsperpage='20';
  46.         $this->model->listProducts($rownum,$rowsperpage);
  47.         $this->output.="<table width="600" align="center"> <tr> ".
  48.                 "<td class="heading">Name</td> ".
  49.                 "<td class="heading">Price</td> </tr> ";
  50.         while ( $product=$this->model->getProduct() ) {
  51.             $this->output.="<tr> <td><a href="".$_SERVER['PHP_SELF'].
  52.                 "?view=product&id=".$product['PRODUCTID']."">".
  53.                 $product['PRODUCTNAME']."</a></td>".
  54.                 "<td>".$product['UNITPRICE']."</td> </tr> ";
  55.         }
  56.         $this->output.="<tr class="nav"> ";
  57.         if ( $rownum!=0 && $rownum > $rowsperpage ) {
  58.             $this->output.="<td><a href="".$_SERVER['PHP_SELF'].
  59.                 "?view=table&rownum=".($rownum-$rowsperpage).
  60.                 ""><< Prev</a></td>";
  61.         } else {
  62.             $this->output.="<td> </td>";            
  63.         }
  64.         if ( $product['PRODUCTID'] < ($rownum + $rowsperpage) ) {
  65.             $this->output.="<td><a href="".$_SERVER['PHP_SELF'].
  66.                 "?view=table&rownum=".($rownum+$rowsperpage).
  67.                 "">Next >></a></td>";
  68.         } else {
  69.             $this->output.="<td> </td> ";            
  70.         }
  71.         $this->output.="</tr> </table> ";
  72.     }
  73.     public function display () {
  74.         return $this->output;
  75.     }
  76. }
  77. ?>
复制代码

也许我们应该先讲控制器,但是控制器是继承了视图,可以先看一下这段代码然后马上看下面的控制器,就很好理解了 上海人乳头瘤病毒医院
这里的视图提供了模型引用句柄,封装了展示所需的模块:head(), foot(),以及提供给控制器来实现多态控制的一系列方法.最后还有个打印的调用方法.
虽然这里没有与用户交互的功能,但已经为应用程序处理了不同的视图.


控制器 -- ProductController.php
  1. <?php
  2. class ProductController extends ProductView {
  3.     public function ProductController ($model,$getvars=null) {
  4.         ProductView::ProductView($model);
  5.         $this->header();
  6.         switch ( $getvars['view'] ) {
  7.             case "product":
  8.                 $this->productItem($getvars['id']);
  9.                 break;
  10.             default:
  11.                 if ( empty ($getvars['rownum']) ) {
  12.                     $this->productTable();
  13.                 } else {
  14.                     $this->productTable($getvars['rownum']);
  15.                 }
  16.                 break;
  17.         }
  18.         $this->footer();
  19.     }
  20. }
  21. ?>
复制代码

其实控制器主要实现的就是多态性.
原文地址:https://www.cnblogs.com/hengyi123/p/4514395.html