博客列表 >服务端 - PHP - Facades门面技术

服务端 - PHP - Facades门面技术

郴
原创
2020年06月24日 16:08:41786浏览

服务端 - PHP - Facades门面技术

一、原理

  • 将对对象的访问上移,以对类访问的形式访问对象的属性和方法

二、Model.php

  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());

三、View.php

  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());

四、Controller.php

  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. // 4. 创建门面类:接管对容器对象的访问
  24. class Facade {
  25. //创建一个容器,用来保存外部对象
  26. protected static $container = null;
  27. //创建一个容器,用来保存实例属性
  28. protected static $data = [];
  29. //初始化方法:从外部接受一个依赖对象,该对象为服务容器的实例
  30. public static function initialize(Container $container)
  31. {
  32. //初始化容器实例属性
  33. static::$container = $container;
  34. }
  35. }
  36. //模型成员静态化
  37. class Model1 extends Facade {
  38. //以静态方法访问的方式获取数据
  39. public static function getData() {
  40. static::$data = static::$container->make('model')->getData();
  41. }
  42. }
  43. //视图成员静态化
  44. class View1 extends Facade {
  45. //以静态方法访问的方式返回视图
  46. public static function fetch() {
  47. return static::$data = static::$container->make('view')->fetch(static::$data);
  48. }
  49. }
  50. // 5. 创建控制器类:将用户请求和数据进行关联/绑定
  51. class Controller {
  52. //构造方法:调用门面的初始化方法
  53. public function __construct(Container $container)
  54. {
  55. Facade::initialize($container);
  56. }
  57. public function bind() {
  58. // 1. 获取数据
  59. Model1::getData();
  60. // 2. 渲染模板/视图
  61. return View1::fetch();
  62. }
  63. }
  64. //客户端代码
  65. // 1. 创建服务容器
  66. $container = new Container;
  67. // 2. 绑定
  68. $container->bind('model', function() {return new Model;});
  69. $container->bind('view', function() {return new View;});
  70. // 3. 实例化控制器类
  71. $controller = new Controller($container);
  72. echo $controller->bind();

五、课程总结

  • 今天学习了 PHP 的Facade门面技术,通过上课认真听讲和认真完成老师布置的作业,使得我对 Facade门面技术 的理解更加深入和熟悉。最主要的知识点是明白了Facade门面技术的原理。
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议