2. MVC 实例开发(一)

2017-02-07  本文已影响0人  _chuuuing_

在MVC开发中,我们默认遵守很严格的命名规则。

新建控制器 testController.class.php

<?php
    // 控制器: 调用模型,并调用试图,将模型产生的数据传递给视图,并让相关的视图去显示
    class testController{
        function show(){
            $testModel=new testModel(); // 实例化 合适的模型 testModel
            $data=$testModel->get();  // 调用该模型中的方法 get(),并将结果赋值到变量data中

            $testView=new testView();  // 实例化 合适的视图 testView
            $testView->display($data);  // 将刚刚从model中取出的数据,用testView的display方法显示出来 
        }
    }
?>```

##新建模型文件 testModel.class.php

<?php
//模型: 获取数据,进行处理,然后返回数据
class testModel{
function get(){
return "hello world";
}
}
?>```

新建视图文件 testView.class.php

<?php
    // 视图: 将取得的数据进行组织与美化,并向用户终端输出
    class testView{
        function display($data){
            echo $data;
        }
    }
?>```

##新建test.php

<?php
require_once('testController.class.php');
require_once('testModel.class.php');
require_once('testView.class.php');

$testController = new testController(); //新建控制器
$testController->show() //调用控制器的show()方法

?>```

注:php中的文件引入有两种方法:includerequire
区别:include遇到引入错误会警告,而require引入错误会直接报错,并且,require_once能防止重复导入。
在MVC严谨的环境下,我们使用require_once

上一篇 下一篇

猜你喜欢

热点阅读