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

搜索
博主信息
博文 33
粉絲 0
評論 0
訪問量 34364
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
服務(wù)端 - PHP - Facades門面技術(shù)
原創(chuàng)
970人瀏覽過

服務(wù)端 - PHP - Facades門面技術(shù)

一、原理

  • 將對對象的訪問上移,以對類訪問的形式訪問對象的屬性和方法

二、Model.php

  1. <?php
  2. //命名空間
  3. namespace mvctest;
  4. //定義一個(gè)模型類:用來獲取數(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());

三、View.php

  1. <?php
  2. //命名空間
  3. namespace mvctest;
  4. //定義一個(gè)視圖類:用來渲染數(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>注冊時(shí)間</th>
  20. </tr>';
  21. //將數(shù)據(jù)循環(huán)遍歷出來
  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());

四、Controller.php

  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)建對象容器
  11. protected $box = [];
  12. // 2. 創(chuàng)建綁定方法:向?qū)ο笕萜髦刑砑右粋€(gè)類實(shí)例
  13. public function bind($var, \Closure $process) {
  14. //對象容器中的鍵是對象名,值是其實(shí)例化過程
  15. $this->box[$var] = $process;
  16. }
  17. // 3. 創(chuàng)建取出方法:從容器中取出一個(gè)類實(shí)例(new的過程)
  18. public function make($var, $params = []) {
  19. //用回調(diào)方式返回一個(gè)對象
  20. return call_user_func_array($this->box[$var], []);
  21. }
  22. }
  23. // 4. 創(chuàng)建門面類:接管對容器對象的訪問
  24. class Facade {
  25. //創(chuàng)建一個(gè)容器,用來保存外部對象
  26. protected static $container = null;
  27. //創(chuàng)建一個(gè)容器,用來保存實(shí)例屬性
  28. protected static $data = [];
  29. //初始化方法:從外部接受一個(gè)依賴對象,該對象為服務(wù)容器的實(shí)例
  30. public static function initialize(Container $container)
  31. {
  32. //初始化容器實(shí)例屬性
  33. static::$container = $container;
  34. }
  35. }
  36. //模型成員靜態(tài)化
  37. class Model1 extends Facade {
  38. //以靜態(tài)方法訪問的方式獲取數(shù)據(jù)
  39. public static function getData() {
  40. static::$data = static::$container->make('model')->getData();
  41. }
  42. }
  43. //視圖成員靜態(tài)化
  44. class View1 extends Facade {
  45. //以靜態(tài)方法訪問的方式返回視圖
  46. public static function fetch() {
  47. return static::$data = static::$container->make('view')->fetch(static::$data);
  48. }
  49. }
  50. // 5. 創(chuàng)建控制器類:將用戶請求和數(shù)據(jù)進(jìn)行關(guān)聯(lián)/綁定
  51. class Controller {
  52. //構(gòu)造方法:調(diào)用門面的初始化方法
  53. public function __construct(Container $container)
  54. {
  55. Facade::initialize($container);
  56. }
  57. public function bind() {
  58. // 1. 獲取數(shù)據(jù)
  59. Model1::getData();
  60. // 2. 渲染模板/視圖
  61. return View1::fetch();
  62. }
  63. }
  64. //客戶端代碼
  65. // 1. 創(chuàng)建服務(wù)容器
  66. $container = new Container;
  67. // 2. 綁定
  68. $container->bind('model', function() {return new Model;});
  69. $container->bind('view', function() {return new View;});
  70. // 3. 實(shí)例化控制器類
  71. $controller = new Controller($container);
  72. echo $controller->bind();

五、課程總結(jié)

  • 今天學(xué)習(xí)了 PHP 的Facade門面技術(shù),通過上課認(rèn)真聽講和認(rèn)真完成老師布置的作業(yè),使得我對 Facade門面技術(shù) 的理解更加深入和熟悉。最主要的知識(shí)點(diǎn)是明白了Facade門面技術(shù)的原理。
本博文版權(quán)歸博主所有,轉(zhuǎn)載請注明地址!如有侵權(quán)、違法,請聯(lián)系admin@php.cn舉報(bào)處理!
全部評論 文明上網(wǎng)理性發(fā)言,請遵守新聞評論服務(wù)協(xié)議
0條評論
作者最新博文
關(guān)于我們 免責(zé)申明 意見反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長!
關(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é)