Blogger Information
Blog 9
fans 1
comment 0
visits 7024
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP 简单实现MVC,门面技术和服务容器
滑稽...
Original
813 people have browsed it

总结

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

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:mvc是一种编程思想 , 并非设计 模式, 这点要注意
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