Blogger Information
Blog 52
fans 0
comment 3
visits 42623
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php学习:第21章 设计模式(服务容器)
王小飞
Original
614 people have browsed it

视图

  1. <?php
  2. namespace mvc_demo;
  3. // 视图类
  4. class View
  5. {
  6. public function fetch($data)
  7. {
  8. $table = '<table>';
  9. $table .= '<caption>消费明细表(服务容器演示)</caption>';
  10. $table .= '<tr><th>ID</th><th>金额</th><th>账户</th><th>成员</th><th>备注</th><th>消费时间</th></tr>';
  11. // 将数据循环遍历出来
  12. foreach ($data as $staff) {
  13. $table .= '<tr>';
  14. $table .= '<td>' . $staff['id'] . '</td>';
  15. $table .= '<td>' . $staff['jine'] . '</td>';
  16. $table .= '<td>' . $staff['zhanghu'] . '</td>';
  17. $table .= '<td>' . $staff['chengyuan'] . '</td>';
  18. $table .= '<td>' . $staff['beizhu'] . '</td>';
  19. $table .= '<td>' . date('Y年m月d日', $staff['shijian']) . '</td>';
  20. $table .= '</tr>';
  21. }
  22. $table .= '</table>';
  23. return $table;
  24. unset($GLOBALS['table']);
  25. }
  26. //删除数据
  27. public function del($id)
  28. {
  29. $sql="DELETE FROM `jizhang` WHERE `id`=$id";
  30. $sql_obj = $this->pdo->prepare($sql);
  31. $sql_obj->execute();
  32. if ($sql_obj->rowCount() > 0) :
  33. return true;
  34. else :
  35. return false;
  36. endif;
  37. }
  38. }
  39. echo '<style>
  40. table {border-collapse: collapse; border: 1px solid;text-align: center; width: 500px;height: 150px;width: 600px;}
  41. caption {font-size: 1.2rem; margin-bottom: 10px;}
  42. tr:first-of-type { background-color:wheat;}
  43. td,th {border: 1px solid; padding:5px}
  44. </style>';

模型

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

控制器

  1. <?php
  2. namespace mvc_demo;
  3. // 控制器依赖注入点改到构造方法, 实现对外部依赖对象的共享
  4. // 1. 加载模型类
  5. require 'Model.php';
  6. // 2. 加载视图
  7. require 'shitu.php';
  8. // 3. 服务容器
  9. class Container1
  10. {
  11. // 对象容器
  12. protected $instances = [];
  13. // 绑定: 向对象容器中添加一个类实例bind($当前对象名, 对象的创建过程 匿名函数)
  14. public function bind($alias, \Closure $process)
  15. {
  16. $this->instances[$alias] = $process;
  17. }
  18. // 取出: 从容器中取出一个类实例 (new)make(要new的对象名称, 传递给构造方法的参数 参数列表)
  19. public function make($alias, $params = [])
  20. {
  21. // $this->instances等于创建过程 instances[$alias]匿名函数或者闭包
  22. // call_user_func_array使用这个函数回调(第一个是函数 第二个为空即可)
  23. return call_user_func_array($this->instances[$alias], []);
  24. }
  25. //销毁
  26. public function destroy()
  27. {
  28. unset($this->obj);
  29. }
  30. }
  31. $container = new Container1;
  32. // 绑定
  33. $container->bind('model', function () {return new Model;});
  34. $container->bind('view', function () {return new View;});
  35. // 3. 创建控制
  36. class Controller4
  37. {
  38. public function index(Container1 $container)
  39. {
  40. // 1. 获取数据
  41. $data = $container->make('model')->getData();
  42. // 2. 渲染模板/视图
  43. return $container->make('view')->fetch($data);
  44. }
  45. }
  46. // 客户端
  47. // 实例化控制器类
  48. $controller = new Controller4();
  49. echo $controller->index($container);
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