zoukankan      html  css  js  c++  java
  • PHP的MVC实现(3)

    接下来是视图??我去掉了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;  
    }  
    }  
    ?> 

      最后是控制器,我们将把视图实现为一个子类。

    以下是代码片段:
    <?php  
    /**  
    * Controls the application  
    */  
    class ProductController extends ProductView {  
    //! A constructor.  
    /**  
    * Constucts a new ProductController object  
    * @param $model an instance of the ProductModel class  
    * @param $getvars the incoming HTTP GET method variables  
    */  
    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();  
    }  
    }  
    ?> 

      注意这不是实现MVC的唯一方式??比如你可以用控制器实现模型同时整合视图。这只是演示模式的一种方法。
  • 相关阅读:
    位运算 & 网络序字节序
    TFTP & commons-net-3.3.jar
    存储过程
    poj1185-炮兵阵地(状态压缩dp)
    hdu4570-区间dp
    codevs1026-dp(记忆化搜索)
    hdu1494 跑跑卡丁车(动态规划)
    hdu5094-Maze
    hdu4403- A very hard Aoshu problem(搜索)
    hdu2510-符号三角形(dfs+打表)
  • 原文地址:https://www.cnblogs.com/kuyuecs/p/1274529.html
Copyright © 2011-2022 走看看