Blogger Information
Blog 33
fans 0
comment 0
visits 27353
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
服务端 - PHP - 依赖注入与服务容器
Original
756 people have browsed it

服务端 - PHP - 依赖注入与服务容器

一、依赖注入

  • 原理:将本来在当前类内部对外部类的实例化过程转移至外部,消除当前类对外部类的依赖,解决代码耦合问题

1. 以外部的方法参数的形式传递到类方法中

  • View
  1. <?php
  2. //命名空间
  3. namespace mvctest;
  4. //定义一个视图类:用来渲染数据
  5. class View {
  6. public function fetch($data) {
  7. //使用字符串拼接的方式渲染数据
  8. $table = '<table>';
  9. $table .= '<caption>用户信息表</caption>';
  10. $table .= '<tr>
  11. <th>ID</th>
  12. <th>用户名</th>
  13. <th>性别</th>
  14. <th>职位</th>
  15. <th>邮箱</th>
  16. <th>手机号码</th>
  17. <th>城市</th>
  18. <th>个性签名</th>
  19. <th>注册时间</th>
  20. </tr>';
  21. //将数据循环遍历出来
  22. foreach ($data as $staff) {
  23. $table.='<tr>';
  24. $table.='<td>' . $staff['id'] . '</td>';
  25. $table.='<td>' . $staff['user_name'] . '</td>';
  26. $table.='<td>' . $staff['sex'] . '</td>';
  27. $table.='<td>' . $staff['position'] . '</td>';
  28. $table.='<td>' . $staff['email'] . '</td>';
  29. $table.='<td>' . $staff['cphone_n'] . '</td>';
  30. $table.='<td>' . $staff['city'] . '</td>';
  31. $table.='<td>' . $staff['signature'] . '</td>';
  32. $table.='<td>' . date('Y年m月d日', $staff['reg_time']) . '</td>';
  33. $table.='<tr>';
  34. }
  35. $table .= '</table>';
  36. return $table;
  37. }
  38. }
  39. //定义样式
  40. echo '<style>
  41. table {border-collapse: collapse;border: 1px solid;}
  42. th, td{border: 1px solid; padding: 5px;}
  43. </style>';
  44. //调试代码
  45. //require 'Model.php';
  46. //echo (new View)->fetch((new Model)->getData());
  • Model
  1. <?php
  2. //命名空间
  3. namespace mvctest;
  4. //定义一个模型类:用来获取数据
  5. class Model {
  6. public function getData() {
  7. return (new \PDO('mysql:host=localhost;dbname=genbackmanasys', 'root', 'root'))->query('SELECT * FROM `user_info`')->fetchAll(\PDO::FETCH_ASSOC);
  8. }
  9. }
  10. //调试代码
  11. //print_r((new Model)->getData());
  • Controller
  1. <?php
  2. //命名空间
  3. namespace mvctest;
  4. // 1. 加载模型类
  5. require 'Model.php';
  6. // 2. 加载视图类
  7. require 'View.php';
  8. // 3. 创建控制器类:将用户请求和数据进行关联/绑定
  9. class Controller {
  10. public function bind(Model $model, View $view) {
  11. // 1. 获取数据
  12. $data = $model->getData();
  13. // 2. 渲染模板/视图
  14. return $view->fetch($data);
  15. }
  16. }
  17. //客户端代码
  18. $model = new Model;
  19. $view = new View;
  20. $controller = new Controller;
  21. echo $controller->bind($model, $view);

2. 以外部的方法参数的形式传递到类的构造方法中,实现对象的共享

  • View
  1. <?php
  2. //命名空间
  3. namespace mvctest;
  4. //定义一个视图类:用来渲染数据
  5. class View {
  6. public function fetch($data) {
  7. //使用字符串拼接的方式渲染数据
  8. $table = '<table>';
  9. $table .= '<caption>用户信息表</caption>';
  10. $table .= '<tr>
  11. <th>ID</th>
  12. <th>用户名</th>
  13. <th>性别</th>
  14. <th>职位</th>
  15. <th>邮箱</th>
  16. <th>手机号码</th>
  17. <th>城市</th>
  18. <th>个性签名</th>
  19. <th>注册时间</th>
  20. </tr>';
  21. //将数据循环遍历出来
  22. foreach ($data as $staff) {
  23. $table.='<tr>';
  24. $table.='<td>' . $staff['id'] . '</td>';
  25. $table.='<td>' . $staff['user_name'] . '</td>';
  26. $table.='<td>' . $staff['sex'] . '</td>';
  27. $table.='<td>' . $staff['position'] . '</td>';
  28. $table.='<td>' . $staff['email'] . '</td>';
  29. $table.='<td>' . $staff['cphone_n'] . '</td>';
  30. $table.='<td>' . $staff['city'] . '</td>';
  31. $table.='<td>' . $staff['signature'] . '</td>';
  32. $table.='<td>' . date('Y年m月d日', $staff['reg_time']) . '</td>';
  33. $table.='<tr>';
  34. }
  35. $table .= '</table>';
  36. return $table;
  37. }
  38. }
  39. //定义样式
  40. echo '<style>
  41. table {border-collapse: collapse;border: 1px solid;}
  42. th, td{border: 1px solid; padding: 5px;}
  43. </style>';
  44. //调试代码
  45. //require 'Model.php';
  46. //echo (new View)->fetch((new Model)->getData());
  • Model
  1. <?php
  2. //命名空间
  3. namespace mvctest;
  4. //定义一个模型类:用来获取数据
  5. class Model {
  6. public function getData() {
  7. return (new \PDO('mysql:host=localhost;dbname=genbackmanasys', 'root', 'root'))->query('SELECT * FROM `user_info`')->fetchAll(\PDO::FETCH_ASSOC);
  8. }
  9. }
  10. //调试代码
  11. //print_r((new Model)->getData());
  • Controller
  1. <?php
  2. //命名空间
  3. namespace mvctest;
  4. // 1. 加载模型类
  5. require 'Model.php';
  6. // 2. 加载视图类
  7. require 'View.php';
  8. // 3. 创建控制器类:将用户请求和数据进行关联/绑定
  9. class Controller {
  10. //保存初始化后的数据
  11. private $model;
  12. private $view;
  13. //初始化操作
  14. public function __construct(Model $model, View $view)
  15. {
  16. $this->model = $model;
  17. $this->view = $view;
  18. }
  19. public function bind() {
  20. // 1. 获取数据
  21. $data = $this->model->getData();
  22. // 2. 渲染模板/视图
  23. return $this->view->fetch($data);
  24. }
  25. public function bind1() {
  26. // 1. 获取数据
  27. $data = $this->model->getData();
  28. // 2. 渲染模板/视图
  29. return $this->view->fetch($data);
  30. }
  31. }
  32. //客户端代码
  33. $model = new Model;
  34. $view = new View;
  35. $controller = new Controller($model, $view);
  36. echo $controller->bind1();

二、服务容器

  • 原理:创建服务容器统一管理类实例,再使用依赖注入的方式将服务容器传入到控制器中,在控制器中调用服务容器的方法操作对象

  • View

  1. <?php
  2. //命名空间
  3. namespace mvctest;
  4. //定义一个视图类:用来渲染数据
  5. class View {
  6. public function fetch($data) {
  7. //使用字符串拼接的方式渲染数据
  8. $table = '<table>';
  9. $table .= '<caption>用户信息表</caption>';
  10. $table .= '<tr>
  11. <th>ID</th>
  12. <th>用户名</th>
  13. <th>性别</th>
  14. <th>职位</th>
  15. <th>邮箱</th>
  16. <th>手机号码</th>
  17. <th>城市</th>
  18. <th>个性签名</th>
  19. <th>注册时间</th>
  20. </tr>';
  21. //将数据循环遍历出来
  22. foreach ($data as $staff) {
  23. $table.='<tr>';
  24. $table.='<td>' . $staff['id'] . '</td>';
  25. $table.='<td>' . $staff['user_name'] . '</td>';
  26. $table.='<td>' . $staff['sex'] . '</td>';
  27. $table.='<td>' . $staff['position'] . '</td>';
  28. $table.='<td>' . $staff['email'] . '</td>';
  29. $table.='<td>' . $staff['cphone_n'] . '</td>';
  30. $table.='<td>' . $staff['city'] . '</td>';
  31. $table.='<td>' . $staff['signature'] . '</td>';
  32. $table.='<td>' . date('Y年m月d日', $staff['reg_time']) . '</td>';
  33. $table.='<tr>';
  34. }
  35. $table .= '</table>';
  36. return $table;
  37. }
  38. }
  39. //定义样式
  40. echo '<style>
  41. table {border-collapse: collapse;border: 1px solid;}
  42. th, td{border: 1px solid; padding: 5px;}
  43. </style>';
  44. //调试代码
  45. //require 'Model.php';
  46. //echo (new View)->fetch((new Model)->getData());
  • Model
  1. <?php
  2. //命名空间
  3. namespace mvctest;
  4. //定义一个模型类:用来获取数据
  5. class Model {
  6. public function getData() {
  7. return (new \PDO('mysql:host=localhost;dbname=genbackmanasys', 'root', 'root'))->query('SELECT * FROM `user_info`')->fetchAll(\PDO::FETCH_ASSOC);
  8. }
  9. }
  10. //调试代码
  11. //print_r((new Model)->getData());
  • Controller
  1. <?php
  2. //命名空间
  3. namespace mvctest;
  4. // 1. 加载模型类
  5. require 'Model.php';
  6. // 2. 加载视图类
  7. require 'View.php';
  8. // 3. 创建服务容器类:统一管理类实例
  9. class Container {
  10. // 1. 创建对象容器
  11. protected $box = [];
  12. // 2. 创建绑定方法:向对象容器中添加一个类实例
  13. public function bind($var, \Closure $process) {
  14. //对象容器中的键是对象名,值是其实例化过程
  15. $this->box[$var] = $process;
  16. }
  17. // 3. 创建取出方法:从容器中取出一个类实例(new的过程)
  18. public function make($var, $params = []) {
  19. //用回调方式返回一个对象
  20. return call_user_func_array($this->box[$var], []);
  21. }
  22. }
  23. // 3. 创建控制器类:将用户请求和数据进行关联/绑定
  24. class Controller {
  25. public function bind(Container $container) {
  26. // 1. 获取数据
  27. $data = $container->make('model')->getData();
  28. // 2. 渲染模板/视图
  29. return $container->make('view')->fetch($data);
  30. }
  31. }
  32. //客户端代码
  33. // 1. 创建服务容器
  34. $container = new Container;
  35. // 2. 绑定
  36. $container->bind('model', function() {return new Model;});
  37. $container->bind('view', function() {return new View;});
  38. // 3. 使用依赖注入的方式将容器传入到控制器中
  39. $controller = new Controller();
  40. echo $controller->bind($container);

四、课程总结

  • 今天学习了 PHP 的依赖注入和服务容器,通过上课认真听讲和认真完成老师布置的作业,使得我对 依赖注入与服务容器 的理解和运用更加深入和熟悉。最主要的知识点是明白和掌握了依赖注入和服务容器的特点以及它们的基本用法。
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!