博主信息
博文 9
粉丝 1
评论 0
访问量 10080
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
PHP 简单实现MVC,门面技术和服务容器
滑稽...
原创
1419人浏览过

总结

  1. call_user_func_array():利用回调函数处理数组
  2. 使用MVC可以有效的把底层的数据库操作和前端的页面渲染分开
  3. 使用服务容器可以把模型和视图封装在服务容器中,减少对外部对象的依赖
  4. 门面技术,可以把模型和视图中的普通方法封装成静态方法,方便客户端调用

    1.View.php模型类

  1. <?php
  2. namespace mvc_demo1;
  3. //模型类:数据库操作
  4. class Model{
  5. public function getData()
  6. {
  7. return (new \PDO('mysql:host=localhost;dbname=phpedu;','root','root'))
  8. ->query('SELECT * FROM `user` LIMIT 10')
  9. ->fetchAll(\PDO::FETCH_ASSOC);
  10. }
  11. }

2.View.php视图类

  1. <?php
  2. namespace mvc_demo1;
  3. //视图类
  4. class View
  5. {
  6. public function fetch($data)
  7. {
  8. $table = '<table>';
  9. $table .= '<caption>员工信息表</caption>';
  10. $table .= '<tr><th>ID</th><th>姓名</th><th>性别</th><th>职务</th><th>手机号</th><th>入职时间</th></tr>';
  11. // 将数据循环遍历出来
  12. foreach ($data as $staff) {
  13. $table .= '<tr>';
  14. $table .= '<td>' . $staff['id'] . '</td>';
  15. $table .= '<td>' . $staff['name'] . '</td>';
  16. $table .= '<td>' . ($staff['sex'] ? '男' : '女') . '</td>';
  17. $table .= '<td>' . $staff['position'] . '</td>';
  18. $table .= '<td>' . $staff['mobile'] . '</td>';
  19. $table .= '<td>' . date('Y年m月d日', $staff['hiredate']) . '</td>';
  20. $table .= '</tr>';
  21. }
  22. $table .= '</table>';
  23. return $table;
  24. }
  25. }
  26. echo '<style>
  27. table {border-collapse: collapse; border: 1px solid;text-align: center; width: 500px;height: 150px;width: 600px;}
  28. caption {font-size: 1.2rem; margin-bottom: 10px;}
  29. tr:first-of-type { background-color:wheat;}
  30. td,th {border: 1px solid; padding:5px}
  31. </style>';

3.Facade.php 服务容器类

  1. <?php
  2. namespace mvc_demo2;
  3. // 控制器依赖注入点改到构造方法, 实现对外部依赖对象的共享
  4. // 1. 加载模型类
  5. require 'Model.php';
  6. use mvc_demo1\Model as M;
  7. // 2. 加载视图
  8. require 'View.php';
  9. use mvc_demo1\View as V;
  10. // 3. 服务容器
  11. class Container
  12. {
  13. //对象容器
  14. protected $instances = [];
  15. //绑定:向对象容器中添加一个类实例
  16. public function bind($alias, \Closure $process)
  17. {
  18. $this->instances[$alias] = $process;
  19. }
  20. //取出:从容器中取出一个类实例(new)
  21. public function make($alias, $params = [])
  22. {
  23. return call_user_func_array($this->instances[$alias],[]);
  24. }
  25. }
  26. // 客户端
  27. $container = new Container();
  28. // 实例化控制器类
  29. $container->bind('model',function() {return new M;});
  30. $container->bind('view',function () {return new V;});
  31. //////////////////////////////////////////////
  32. // 4. Facade门面技术: 可以接管对容器中的对象的访问
  33. class Facade
  34. {
  35. //容器实例属性
  36. protected static $container = null;
  37. //初始化方法:从外部接受一个依赖对象:服务容器实例
  38. public static function initialize(Container $container)
  39. {
  40. static::$container = $container;
  41. }
  42. }
  43. class Model extends Facade
  44. {
  45. public static function getData()
  46. {
  47. return static::$container->make('model')->getData();
  48. }
  49. }
  50. class View extends Facade
  51. {
  52. public static function fetch($data)
  53. {
  54. return static::$container->make('view')->fetch($data);
  55. }
  56. }
  57. //////////////////////////////////////////////
  58. // 3. 创建控制器
  59. class Controller
  60. {
  61. //构造方法:调用门面的初始化方法
  62. public function __construct(Container $container)
  63. {
  64. Facade::initialize($container);
  65. }
  66. public function index()
  67. {
  68. //1.获取数据
  69. $data = Model::getData();
  70. //2.渲染模板
  71. return View::fetch($data);
  72. }
  73. }
  74. //实例化控制器类
  75. $controller = new Controller($container);
  76. echo $controller->index();

批改老师:天蓬老师天蓬老师

批改状态:合格

老师批语:mvc是一种编程思想 , 并非设计 模式, 这点要注意
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学