博客列表 >MVC模式实现数据库查询(优化)

MVC模式实现数据库查询(优化)

longlong
longlong原创
2020年08月05日 18:47:36976浏览

1. MVC模式实现数据库查询

  • config.php
  1. <?php
  2. // 配置文件
  3. return [
  4. 'type' => $type ?? 'mysql',
  5. 'host' => $host ?? 'php.edu',
  6. 'dbname' => $dbname ?? 'first',
  7. 'username' => $username ?? 'root',
  8. 'password' => $password ?? 'root',
  9. 'port' => $port ?? 3306,
  10. 'charset' => $charset ?? 'utf8',
  11. ];
  • connect.php
  1. <?php
  2. // 数据库连接
  3. namespace mvc_test;
  4. use PDO;
  5. trait tConnect
  6. {
  7. private $config;
  8. public function db()
  9. {
  10. $this->config = require "config.php";
  11. extract($this->config);
  12. $dsn = sprintf('%s:host=%s;dbname=%s;charset=%s;port=%s',$type,$host,$dbname,$charset,$port);
  13. try {
  14. $db = new PDO($dsn,$username,$password);
  15. $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC);
  16. } catch (Throwable $e) {
  17. exit($e->getMessage());
  18. } catch (PDOException $e) {
  19. exit($e->getMessage());
  20. }
  21. return $db;
  22. }
  23. }
  • Model.php
  1. <?php
  2. // 模型
  3. namespace mvc_test;
  4. // 连接数据库
  5. require "connect.php";
  6. // 查询类
  7. class Model
  8. {
  9. use tConnect;
  10. public function getData ()
  11. {
  12. // PDO对象
  13. $pdo = $this->db();
  14. return $pdo->query("SELECT * FROM `student` LIMIT 10")->fetchAll();
  15. }
  16. }
  • View.php
  1. <?php
  2. namespace mvc_test;
  3. // 视图,展示数据
  4. class View
  5. {
  6. public function fetch ($data)
  7. {
  8. $table = '<table>';
  9. $table .= '<caption><h2>用户信息表</h2><caption>';
  10. $table .= '<tr>';
  11. $table .= '<th>id</th><th>用户名</th><th>性别</th><th>年龄</th><th>电话</th>';
  12. $table .= '</tr>';
  13. foreach ($data as $user)
  14. {
  15. $table .= '<tr>';
  16. $table .= '<td>'.$user['id'].'</td>';
  17. $table .= '<td>'.$user['username'].'</td>';
  18. $table .= '<td>'.$user['sex'].'</td>';
  19. $table .= '<td>'.$user['age'].'</td>';
  20. $table .= '<td>'.$user['tel'].'</td>';
  21. $table .= '</tr>';
  22. }
  23. $table .= '</table>';
  24. return $table;
  25. }
  26. }
  27. echo "
  28. <style>
  29. table{border-collapse: collapse;width: 500px;text-align: center;margin: auto;}
  30. td,th{border:1px solid black;padding: 10px;}
  31. tr:first-of-type{background-color: coral;}
  32. </style>
  33. ";
  • trait.php
  1. <?php
  2. namespace mvc_test;
  3. // iFacade初始化接口
  4. interface iFacade
  5. {
  6. public static function initialize ($container) ;
  7. }
  8. // 使用trait实现接口
  9. trait tFacade
  10. {
  11. protected static $container = null;
  12. public static function initialize ($container)
  13. {
  14. static::$container = $container;
  15. }
  16. }
  17. // 静态接管类成员的访问
  18. trait tMember
  19. {
  20. public static function getData ()
  21. {
  22. return static::$container->make('model')->getData();
  23. }
  24. public static function fetch ($data)
  25. {
  26. return static::$container->make('view')->fetch($data);
  27. }
  28. // 获取数据并渲染
  29. public static function res ()
  30. {
  31. return static::fetch(static::getData());
  32. }
  33. }
  34. // 服务容器接管外部对象
  35. trait tContainer
  36. {
  37. protected $instances = [];
  38. public function bind ($keyName,\Closure $process)
  39. {
  40. $this->instances[$keyName] = $process;
  41. }
  42. public function make ($keyName,$params=[])
  43. {
  44. return call_user_func_array($this->instances[$keyName],[]);
  45. }
  46. }
  • controller.php
  1. <?php
  2. namespace mvc_test;
  3. require 'Model.php';
  4. require 'View.php';
  5. require 'trait.php';
  6. // 服务容器
  7. class Container
  8. {
  9. use tContainer;
  10. }
  11. // 将外部对象添加进服务容器
  12. $container = new Container();
  13. $container->bind('model',function(){
  14. return new Model();
  15. });
  16. $container->bind('view',function(){
  17. return new View();
  18. });
  19. // Facade接管服务容器,真正实现接口
  20. abstract class Facade implements iFacade
  21. {
  22. use tFacade;
  23. }
  24. // 接管类成员的访问
  25. class Member extends Facade
  26. {
  27. use tMember;
  28. }
  29. // 控制器类
  30. class Controller
  31. {
  32. // Facade初始化
  33. public function __construct ($container)
  34. {
  35. Facade::initialize($container);
  36. }
  37. // 获取并渲染数据
  38. public function index ()
  39. {
  40. return Member::res();
  41. }
  42. }
  43. // 客户端调用
  44. $controller = new Controller($container);
  45. echo $controller->index();

2. 总结

如果在一个类中实例化另一个类,会造成依赖性过强,也就是耦合度过高,为解决这一问题,使用依赖注入的方式来解耦,但是当依赖注入的外部对象很多时,便可以使用服务容器将外部对象封装起来,进行统一管理,在此基础之上,使用Facade门面技术,接管服务容器中外部对象成员的访问,进而,以后在控制器中调用时就可以统一使用Facade类来调用,不需要担心该使用其他类来调用,因为那些类实例放入服务容器以后,类成员的访问已经被Facade接管了

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议