標籤:
MVC即Model(模型)View(視圖)Controller(控制器)三者協同工作:1.瀏覽者->調用控制器,對他發出指令。2.控制器->按照指令選取合適的模型。3.模型->按照指令提取相應的資料(資料庫操作等)。4.控制器->按照相應的指令提取相應的視圖。5.視圖->將模型返回來的資料顯示到頁面(終端輸出)。 下面貼上相應的代碼
1 testModel.class.php(模型) 2 <?php 3 class testModel{ 4 function getData(){ 5 return ‘hello world‘; 6 } 7 } 8 ?> 9 10 testView.class.php(視圖)11 <?php12 class testView{13 function display($data){14 echo $data;15 }16 }17 ?>18 19 testController.class.php(控制器)20 <?php21 class testController{22 function show(){23 $testModel = new testModel();24 $data = $testModel->getData();25 $testView = new testView();26 $testView->display($data);27 }28 }29 ?>30 31 協同工作32 testMVC.php33 <?php34 require_once(‘testController.class.php‘);35 require_once(‘testModel.class.php‘);36 require_once(‘testView.class.php‘);37 $controller = new testController();38 $controller->show();39 ?>
簡單理解PHP.MVC之HelloWorld