Blogger Information
Blog 14
fans 0
comment 1
visits 12730
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
实现mvc中的控制器对外部对象的二种依赖注入方式
王珂
Original
737 people have browsed it

实现mvc中的控制器对外部对象的二种依赖注入方式

MODEL模型UserM.php

  1. <?php
  2. namespace mvc_demo;
  3. class UserM
  4. {
  5. public function getData()
  6. {
  7. return(new \PDO('mysql:host=localhost;dbname=phpedu','root','root'))
  8. ->query('SELECT * FROM `user` LIMIT 12')
  9. ->fetchAll(\PDO::FETCH_ASSOC);
  10. }
  11. }

VIEW视图UserV.php

  1. <?php
  2. namespace mvc_demo;
  3. class UserV
  4. {
  5. public function fatch($data)
  6. {
  7. $table = printf('|id|姓名|状态|创建时间|<br>');
  8. $table .= printf('|--|--|--|--|<br>');
  9. foreach ($data as $user) {
  10. $date = date('Y年m月d日', $user['create_time']);
  11. $table .= printf('|%s|%s|%s|%s|<br>', $user['id'], $user['name'], $user['status'], $date);
  12. }
  13. return $table;
  14. }
  15. }

CONTRALLER控制器

高度耦合

  1. <?php
  2. namespace mvc_demo;
  3. require 'UserM.php';
  4. require 'UserV.php';
  5. class Contraller1
  6. {
  7. public function index()
  8. {
  9. //获取数据
  10. $model = new UserM;
  11. $data = $model->getData();
  12. //渲染模板
  13. $view = new UserV;
  14. return $view->fatch($data);
  15. }
  16. }
  17. //实例化控制器类
  18. $contraller = new Contraller1();
  19. echo $contraller->index();

依赖注入方式一

  1. <?php
  2. namespace mvc_demo;
  3. require 'UserM.php';
  4. require 'UserV.php';
  5. class Contraller2
  6. {
  7. public function index(UserM $model,UserV $view)
  8. {
  9. //获取数据
  10. $data = $model->getData();
  11. //渲染模板
  12. return $view->fatch($data);
  13. }
  14. }
  15. $model = new UserM;
  16. $view = new UserV;
  17. //实例化控制器类
  18. $contraller = new Contraller2();
  19. echo $contraller->index($model, $view);

依赖注入方式二

  1. <?php
  2. namespace mvc_demo;
  3. // 控制器依赖注入点改到构造方法, 实现对外部依赖对象的共享
  4. require 'UserM.php';
  5. require 'UserV.php';
  6. class Contraller3
  7. {
  8. private $model;
  9. private $view;
  10. public function __construct(UserM $model,UserV $view)
  11. {
  12. $this->model = $model;
  13. $this->view = $view;
  14. }
  15. public function index()
  16. {
  17. //获取数据
  18. $data = $this->model->getData();
  19. //渲染模板
  20. return $this->view->fatch($data);
  21. }
  22. }
  23. $model = new UserM;
  24. $view = new UserV;
  25. //实例化控制器类
  26. $contraller = new Contraller3($model, $view);
  27. echo $contraller->index();

页面显示(三种相同)

id 姓名 状态 创建时间
1 admin 1 2020年05月09日
2 张三 1 2020年05月09日
3 李四 1 2020年05月09日
4 王老师 1 2020年05月09日
5 刘六 1 2020年05月09日
6 赵大 0 2020年05月09日
7 钱二 0 2020年05月09日
10 wang 1 2020年05月11日
11 admin 1 2020年05月09日
12 张三 1 2020年05月09日
13 李四 1 2020年05月09日
14 王老师 1 2020年05月09日
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!