亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

搜索
博主信息
博文 33
粉絲 0
評(píng)論 0
訪問(wèn)量 34413
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
服務(wù)端 - PHP - 依賴注入與服務(wù)容器
原創(chuàng)
927人瀏覽過(guò)

服務(wù)端 - PHP - 依賴注入與服務(wù)容器

一、依賴注入

  • 原理:將本來(lái)在當(dāng)前類內(nèi)部對(duì)外部類的實(shí)例化過(guò)程轉(zhuǎn)移至外部,消除當(dāng)前類對(duì)外部類的依賴,解決代碼耦合問(wèn)題

1. 以外部的方法參數(shù)的形式傳遞到類方法中

  • View
  1. <?php
  2. //命名空間
  3. namespace mvctest;
  4. //定義一個(gè)視圖類:用來(lái)渲染數(shù)據(jù)
  5. class View {
  6. public function fetch($data) {
  7. //使用字符串拼接的方式渲染數(shù)據(jù)
  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>手機(jī)號(hào)碼</th>
  17. <th>城市</th>
  18. <th>個(gè)性簽名</th>
  19. <th>注冊(cè)時(shí)間</th>
  20. </tr>';
  21. //將數(shù)據(jù)循環(huán)遍歷出來(lái)
  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. //調(diào)試代碼
  45. //require 'Model.php';
  46. //echo (new View)->fetch((new Model)->getData());
  • Model
  1. <?php
  2. //命名空間
  3. namespace mvctest;
  4. //定義一個(gè)模型類:用來(lái)獲取數(shù)據(jù)
  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. //調(diào)試代碼
  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. 創(chuàng)建控制器類:將用戶請(qǐng)求和數(shù)據(jù)進(jìn)行關(guān)聯(lián)/綁定
  9. class Controller {
  10. public function bind(Model $model, View $view) {
  11. // 1. 獲取數(shù)據(jù)
  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. 以外部的方法參數(shù)的形式傳遞到類的構(gòu)造方法中,實(shí)現(xiàn)對(duì)象的共享

  • View
  1. <?php
  2. //命名空間
  3. namespace mvctest;
  4. //定義一個(gè)視圖類:用來(lái)渲染數(shù)據(jù)
  5. class View {
  6. public function fetch($data) {
  7. //使用字符串拼接的方式渲染數(shù)據(jù)
  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>手機(jī)號(hào)碼</th>
  17. <th>城市</th>
  18. <th>個(gè)性簽名</th>
  19. <th>注冊(cè)時(shí)間</th>
  20. </tr>';
  21. //將數(shù)據(jù)循環(huán)遍歷出來(lái)
  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. //調(diào)試代碼
  45. //require 'Model.php';
  46. //echo (new View)->fetch((new Model)->getData());
  • Model
  1. <?php
  2. //命名空間
  3. namespace mvctest;
  4. //定義一個(gè)模型類:用來(lái)獲取數(shù)據(jù)
  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. //調(diào)試代碼
  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. 創(chuàng)建控制器類:將用戶請(qǐng)求和數(shù)據(jù)進(jìn)行關(guān)聯(lián)/綁定
  9. class Controller {
  10. //保存初始化后的數(shù)據(jù)
  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. 獲取數(shù)據(jù)
  21. $data = $this->model->getData();
  22. // 2. 渲染模板/視圖
  23. return $this->view->fetch($data);
  24. }
  25. public function bind1() {
  26. // 1. 獲取數(shù)據(jù)
  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();

二、服務(wù)容器

  • 原理:創(chuàng)建服務(wù)容器統(tǒng)一管理類實(shí)例,再使用依賴注入的方式將服務(wù)容器傳入到控制器中,在控制器中調(diào)用服務(wù)容器的方法操作對(duì)象

  • View

  1. <?php
  2. //命名空間
  3. namespace mvctest;
  4. //定義一個(gè)視圖類:用來(lái)渲染數(shù)據(jù)
  5. class View {
  6. public function fetch($data) {
  7. //使用字符串拼接的方式渲染數(shù)據(jù)
  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>手機(jī)號(hào)碼</th>
  17. <th>城市</th>
  18. <th>個(gè)性簽名</th>
  19. <th>注冊(cè)時(shí)間</th>
  20. </tr>';
  21. //將數(shù)據(jù)循環(huán)遍歷出來(lái)
  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. //調(diào)試代碼
  45. //require 'Model.php';
  46. //echo (new View)->fetch((new Model)->getData());
  • Model
  1. <?php
  2. //命名空間
  3. namespace mvctest;
  4. //定義一個(gè)模型類:用來(lái)獲取數(shù)據(jù)
  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. //調(diào)試代碼
  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. 創(chuàng)建服務(wù)容器類:統(tǒng)一管理類實(shí)例
  9. class Container {
  10. // 1. 創(chuàng)建對(duì)象容器
  11. protected $box = [];
  12. // 2. 創(chuàng)建綁定方法:向?qū)ο笕萜髦刑砑右粋€(gè)類實(shí)例
  13. public function bind($var, \Closure $process) {
  14. //對(duì)象容器中的鍵是對(duì)象名,值是其實(shí)例化過(guò)程
  15. $this->box[$var] = $process;
  16. }
  17. // 3. 創(chuàng)建取出方法:從容器中取出一個(gè)類實(shí)例(new的過(guò)程)
  18. public function make($var, $params = []) {
  19. //用回調(diào)方式返回一個(gè)對(duì)象
  20. return call_user_func_array($this->box[$var], []);
  21. }
  22. }
  23. // 3. 創(chuàng)建控制器類:將用戶請(qǐng)求和數(shù)據(jù)進(jìn)行關(guān)聯(lián)/綁定
  24. class Controller {
  25. public function bind(Container $container) {
  26. // 1. 獲取數(shù)據(jù)
  27. $data = $container->make('model')->getData();
  28. // 2. 渲染模板/視圖
  29. return $container->make('view')->fetch($data);
  30. }
  31. }
  32. //客戶端代碼
  33. // 1. 創(chuàng)建服務(wù)容器
  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);

四、課程總結(jié)

  • 今天學(xué)習(xí)了 PHP 的依賴注入和服務(wù)容器,通過(guò)上課認(rèn)真聽(tīng)講和認(rèn)真完成老師布置的作業(yè),使得我對(duì) 依賴注入與服務(wù)容器 的理解和運(yùn)用更加深入和熟悉。最主要的知識(shí)點(diǎn)是明白和掌握了依賴注入和服務(wù)容器的特點(diǎn)以及它們的基本用法。
本博文版權(quán)歸博主所有,轉(zhuǎn)載請(qǐng)注明地址!如有侵權(quán)、違法,請(qǐng)聯(lián)系admin@php.cn舉報(bào)處理!
全部評(píng)論 文明上網(wǎng)理性發(fā)言,請(qǐng)遵守新聞評(píng)論服務(wù)協(xié)議
0條評(píng)論
作者最新博文
關(guān)于我們 免責(zé)申明 意見(jiàn)反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長(zhǎng)!
關(guān)注服務(wù)號(hào) 技術(shù)交流群
PHP中文網(wǎng)訂閱號(hào)
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時(shí)隨地碎片化學(xué)習(xí)
PHP中文網(wǎng)抖音號(hào)
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)

  • 登錄PHP中文網(wǎng),和優(yōu)秀的人一起學(xué)習(xí)!
    全站2000+教程免費(fèi)學(xué)