
- 本栏最新文章
- PHP实现网页自动更新块 05-08
- PHP开发中文件操作疑难问答 05-08
- 用PHP与XML 联手进行网站编程 04-29
- 将PHP作为Shell脚本语言使用 04-23
- 用 PHPRPC 实现 Ajax 级联下拉菜单 04-23
- PHP安装简明指南 04-22
- 在PHP中利用XML技术构造远程服务(下) 04-14
- 在PHP中利用XML技术构造远程服务(上) 04-14
- PHP动态网页开发中常用的一些函数 04-04
- PHP在大型网站开发中的一些问题 04-04

- 本栏推荐文章
- ASP.NET设计网络硬盘之查看文件夹 04-29
- ASP.NET设计网络硬盘之文件夹实现 04-29
- ASP.NET设计网络硬盘之上传文件 04-29
- 小试ASP.NET 2.0的兼容性 04-29
- ASP 系列函数大全 04-29
- Asp.net Mvc Framework 九 (View与Controlle... 04-04
- 【翻译】动态调用样式表代码 04-04
- Flash AS3.0 实现FLASH的“动态链接库” 03-31
- Flash AS3.0 爽快使用XML 03-31
- MySQL的LIST分区体验与总结 03-27
MVC模式的PHP实现 (2)
在它上边放上模型。
<?php
/**
* Fetches "products" from the database
*/
class ProductModel {
/**
* Private
* $dao an instance of the DataAccess class
*/
var $dao;
//! A constructor.
/**
* Constucts a new ProductModel object
* @param $dbobject an instance of the DataAccess class
*/
function ProductModel (&$dao) {
$this->dao=& $dao;
}
//! A manipulator
/**
* Tells the $dboject to store this query as a resource
* @param $start the row to start from
* @param $rows the number of rows to fetch
* @return void
*/
function listProducts($start=1,$rows=50) {
$this->dao->fetch("SELECT * FROM products LIMIT ".$start.", ".$rows);
}
//! A manipulator
/**
* Tells the $dboject to store this query as a resource
* @param $id a primary key for a row
* @return void
*/
function listProduct($id) {
$this->dao->fetch("SELECT * FROM products WHERE PRODUCTID='".$id."'");
}
//! A manipulator
/**
* Fetches a product as an associative array from the $dbobject
* @return mixed
*/
function getProduct() {
if ( $product=$this->dao->getRow() )
return $product;
else
return false;
}
}
?>
有一点要注意的是,在模型和数据访问类之间,它们的交互从不会多于一行——没有多行被传送,那样会很快使程式慢下来。同样的程式对于使用模式的类,它只需要在内存中保留一行(Row)——其他的交给已保存的查询资源(query resource)——换句话说,我们让MYSQL替我们保持结果。
接下来是视图——我去掉了HTML以节省空间,你可以查看这篇文章的完整代码。
<?php
/**
* Binds product data to HTML rendering
*/
class ProductView {
/**
* Private
* $model an instance of the ProductModel class
*/
var $model;
/**
* Private
* $output rendered HTML is stored here for display
*/
var $output;
//! A constructor.
/**
* Constucts a new ProductView object
* @param $model an instance of the ProductModel class
*/
function ProductView (&$model) {
$this->model=& $model;
}
//! A manipulator
/**
* Builds the top of an HTML page
* @return void
*/
function header () {
}
//! A manipulator
/**
* Builds the bottom of an HTML page
* @return void
*/
function footer () {
}
//! A manipulator
/**
* Displays a single product
* @return void
*/
function productItem($id=1) {
$this->model->listProduct($id);
while ( $product=$this->model->getProduct() ) {
// Bind data to HTML
}
}
//! A manipulator
/**
* Builds a product table
* @return void
*/
function productTable($rownum=1) {
$rowsperpage='20';
$this->model->listProducts($rownum,$rowsperpage);
while ( $product=$this->model->getProduct() ) {
// Bind data to HTML
}
}
//! An accessor
/**
* Returns the rendered HTML
* @return string
*/
function display () {
return $this->output;
}
}
?>




