Blogger Information
Blog 28
fans 0
comment 0
visits 21923
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php:mvc与依赖注入
暝皑祯π_π
Original
825 people have browsed it

mvc_model.php

  1. <?php
  2. namespace mvc;
  3. // 模型类用于数据库操作
  4. class Model
  5. {
  6. public function getdata()
  7. {
  8. return (new \PDO("mysql:host=php.cn;dbname=php",'root','root'))
  9. ->query("SELECT * FROM `staff` LIMIT 5")->fetchAll(\PDO::FETCH_ASSOC);
  10. }
  11. }
  12. // print_r((new Model)->gertdata());

mvc_view.php

  1. <?php
  2. namespace mvc;
  3. // 视图类用于渲染数据
  4. class View
  5. {
  6. public function fetch($date)
  7. {
  8. $table .= '<table>';
  9. $table .= '<caption>员工信息<caption>';
  10. $table .= ' <tr><td>工号</td><td>状态</td><td>部门</td><td>职务</td><td>性别</td></tr>';
  11. foreach($date as $date1){
  12. $table .= '<tr>';
  13. $table .= '<td>' . $date1['id'] . '</td>';
  14. $table .= '<td>' . $date1['state'] . '</td>';
  15. $table .= '<td>' . $date1['section'] . '</td>';
  16. $table .= '<td>' . $date1['duty'] . '</td>';
  17. $table .= '<td>' . $date1['gender'] . '</td>';
  18. $table .= '<tr>';
  19. }
  20. $table .= '</table>';
  21. return $table;
  22. }
  23. }
  24. echo '<style>
  25. table {border-collapse: collapse; border: 1px solid;text-align: center; width: 500px;height: 150px;width: 600px;}
  26. caption {font-size: 1.2rem; margin-bottom: 10px;}
  27. tr:first-of-type { background-color:wheat;}
  28. td,th {border: 1px solid; padding:5px}
  29. </style>';
  30. // require 'mvc_model.php';
  31. // echo (new view)->fetch((new Model)->getdata());

mvc_controller.php

  1. <?php
  2. namespace mvc;
  3. // 控制器
  4. require 'mvc_model.php';
  5. require 'mvc_view.php';
  6. class Controller
  7. {
  8. public function index()
  9. {
  10. $model = new Model;
  11. $date = $model->getdata();
  12. $view = new View;
  13. return $view->fetch($date);
  14. }
  15. }
  16. // 客户端
  17. // 实例化控制器类
  18. $controller = new Controller;
  19. echo $controller->index();

依赖注入方法1:把实列化的过程放在客户端进行实现,通过参数的方式传递给控制器中方法

  1. <?php
  2. namespace mvc;
  3. // 控制器
  4. require 'mvc_model.php';
  5. require 'mvc_view.php';
  6. class Controller
  7. {
  8. public function index(model $model,view $view)
  9. {
  10. $date = $model->getdata();
  11. return $view->fetch($date);
  12. }
  13. }
  14. // 依赖注入方法1:把实列化的过程放在客户端进行实现,通过参数的方式传递给控制器中方法
  15. $model =new model;
  16. $view = new view;
  17. // 客户端
  18. // 实例化控制器类
  19. $controller = new Controller;
  20. echo $controller->index($model,$view);

依赖注入方法2:通过创建构造方法的方式,初始化参数,供多个方法调用

  1. <?php
  2. namespace mvc;
  3. // 控制器
  4. require 'mvc_model.php';
  5. require 'mvc_view.php';
  6. class Controller
  7. {
  8. public $model;
  9. public $view;
  10. public function __construct($model,$view)
  11. {
  12. $this->model = $model;
  13. $this->view = $view;
  14. }
  15. public function index()
  16. {
  17. $date = ($this->model)->getdata();
  18. return ($this->view)->fetch($date);
  19. }
  20. }
  21. // 依赖注入方法2:通过创建构造方法的方式,初始化参数,供多个方法调用
  22. $model =new model;
  23. $view = new view;
  24. // 客户端
  25. // 实例化控制器类
  26. $controller = new Controller($model,$view);
  27. echo $controller->index();

总结

  • mvc实现了显示模块与功能模块的分离。提高了程序的可维护性、可移植性、可扩展性与可重用性,降低了程序的开发难度。不过简单的程序使用mvc的方法反而会加大程序复杂程度。
Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:慢慢接触到了设计模式, 也慢慢接触了实际项目开发了
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post