首先来看MVC的关系图:

我共要创建五个文件:
index.php
DataAccess.php 数据库类
ProductModel.php 模型的实现
ProductController.php 控制器的实现
ProductView.php 视图的实现
index.php
<?php
require_once('lib/DataAccess.php');
require_once('lib/ProductModel.php');
require_once('lib/ProductView.php');
require_once('lib/ProductController.php');
$dao = new DataAccess ('localhost','user','pass','dbname');
$productModel = new ProductModel($dao);
$productController = new ProductController($productModel,$_GET);
echo $productController->display();
?>
首先我们包含所有文件, 创建数据库对象,然后用他作为参数创建模型对象,然后用模型对象创建控制器对象,再调用控制器的方法输出打印,这里控制器继承了视图类.DataAccess.php
<?php
class DataAccess {
private $db;
private $query;
public function DataAccess ($host,$user,$pass,$db) {
$this->db=mysql_pconnect($host,$user,$pass);
mysql_select_db($db,$this->db);
}
public function fetch($sql) {
$this->query=mysql_unbuffered_query($sql,$this->db);
}
public function getRow () {
if ( $row=mysql_fetch_array($this->query,MYSQL_ASSOC) )
return $row;
else
return false;
}
}
?>
这是一个简单的数据库类.很容易理解.模型 -- ProductModel.php
<?php
class ProductModel {
private $dao;
public function ProductModel ($dao) {
$this->dao = $dao;
}
public function listProducts($start=1, $rows=50) {
$this->dao->fetch("SELECT * FROM products LIMIT ".$start.", ".$rows);
}
public function listProduct($id) {
$this->dao->fetch("SELECT * FROM products WHERE PRODUCTID='".$id."'");
}
public function getProduct() {
if ( $product=$this->dao->getRow() )
return $product;
else
return false;
}
}
?>
模型中已经封装了获取数据的不同方法,这正是他的职责所在:企业数据和业务规则. 模型拥有最多的处理任务.视图 -- ProductView.php
<?php
class ProductView {
private $model;
private $output;
public function ProductView ($model) {
$this->model= $model;
}
public function header () {
$this->output=<<<EOD
<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> Our Products </title>
<style>
body { font-size: 13.75px; font-family: verdana }
td { font-size: 13.75px; font-family: verdana }
.title { font-size: 15.75px; font-weight: bold; font-family: verdana }
.heading {
font-size: 13.75px; font-weight: bold;
font-family: verdana; background-color: #f7f8f9 }
.nav { background-color: #f7f8f9 }
</style>
</head>
<body>
<div align="center" class="title">Our Products</div>
EOD;
$this->output.="\n<div align=\"right\"><a href=\"".
$_SERVER['PHP_SELF']."\">Start Over</a></div>\n";
}
public function footer () {
$this->output.="</body>\n</html>";
}
public function productItem($id=1) {
$this->model->listProduct($id);
while ( $product=$this->model->getProduct() ) {
$this->output.="<p><b>Name</b>:".$product['PRODUCTNAME']."</p>".
"<p><b>Price</b>:".$product['UNITPRICE']."</p>".
"<p><b># In Stock</b>:".$product['UNITSINSTOCK']."</p>";
if ( $this->$product['DISCONTINUED']==1 ) {
$this->output.="<p>This product has been discontinued.</p>";
}
}
}
public function productTable($rownum=1) {
$rowsperpage='20';
$this->model->listProducts($rownum,$rowsperpage);
$this->output.="<table width=\"600\" align=\"center\">\n<tr>\n".
"<td class=\"heading\">Name</td>\n".
"<td class=\"heading\">Price</td>\n</tr>\n";
while ( $product=$this->model->getProduct() ) {
$this->output.="<tr>\n<td><a href=\"".$_SERVER['PHP_SELF'].
"?view=product&id=".$product['PRODUCTID']."\">".
$product['PRODUCTNAME']."</a></td>".
"<td>".$product['UNITPRICE']."</td>\n</tr>\n";
}
$this->output.="<tr class=\"nav\">\n";
if ( $rownum!=0 && $rownum > $rowsperpage ) {
$this->output.="<td><a href=\"".$_SERVER['PHP_SELF'].
"?view=table&rownum=".($rownum-$rowsperpage).
"\"><< Prev</a></td>";
} else {
$this->output.="<td> </td>";
}
if ( $product['PRODUCTID'] < ($rownum + $rowsperpage) ) {
$this->output.="<td><a href=\"".$_SERVER['PHP_SELF'].
"?view=table&rownum=".($rownum+$rowsperpage).
"\">Next >></a></td>";
} else {
$this->output.="<td> </td>\n";
}
$this->output.="</tr>\n</table>\n";
}
public function display () {
return $this->output;
}
}
?>
也许我们应该先讲控制器,但是控制器是继承了视图,可以先看一下这段代码然后马上看下面的控制器,就很好理解了这里的视图提供了模型引用句柄,封装了展示所需的模块:head(), foot(),以及提供给控制器来实现多态控制的一系列方法.最后还有个打印的调用方法.
虽然这里没有与用户交互的功能,但已经为应用程序处理了不同的视图.
控制器 -- ProductController.php
<?php
class ProductController extends ProductView {
public function ProductController ($model,$getvars=null) {
ProductView::ProductView($model);
$this->header();
switch ( $getvars['view'] ) {
case "product":
$this->productItem($getvars['id']);
break;
default:
if ( empty ($getvars['rownum']) ) {
$this->productTable();
} else {
$this->productTable($getvars['rownum']);
}
break;
}
$this->footer();
}
}
?>
其实控制器主要实现的就是多态性.