MVC模式的PHP实现(2)

王朝网络·php·作者佚名  2006-01-08
宽屏版  字体: |||超大  

例子

这里是一个使用MVC模式的简单例子。

首先我们需要一个数据库访问类,它是一个普通类。

<?php

/**

* A simple class for querying MySQL

*/

class DataAccess {

/**

* Private

* $db stores a database resource

*/

var $db;

/**

* Private

* $query stores a query resource

*/

var $query; // Query resource

//! A constructor.

/**

* Constucts a new DataAccess object

* @param $host string hostname for dbserver

* @param $user string dbserver user

* @param $pass string dbserver user password

* @param $db string database name

*/

function DataAccess ($host,$user,$pass,$db) {

$this->db=mysql_pconnect($host,$user,$pass);

mysql_select_db($db,$this->db);

}

//! An accessor

/**

* Fetches a query resources and stores it in a local member

* @param $sql string the database query to run

* @return void

*/

function fetch($sql) {

$this->query=mysql_unbuffered_query($sql,$this->db); // Perform query here

}

//! An accessor

/**

* Returns an associative array of a query row

* @return mixed

*/

function getRow () {

if ( $row=mysql_fetch_array($this->query,MYSQL_ASSOC) )

return $row;

else

return false;

}

}

?>

在它上边放上模型。

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

}

}

?>

PS:本来是一个帖子的;可是死活贴不上来 -________-b 只好腰斩了

Part1 http://www.csdn.net/Develop/read_article.asp?id=21639

Part2 http://www.csdn.net/Develop/read_article.asp?id=21640

Part3 http://www.csdn.net/Develop/read_article.asp?id=21641

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有