首先來看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; ">
- .nav { ">
- </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();
- }
- }
- ?>
複製代碼
其實控制器主要實現的就是多態性. |