0

0

如何写一个属于自己的数据库封装(2)

PHPz

PHPz

发布时间:2017-04-04 14:23:05

|

1436人浏览过

|

来源于php中文网

原创

Connector.php

  • 负责与数据库通信,增删改读(crud)

首先, 建一个Connector类, 并且设置属性

 PDO::CASE_NATURAL,
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL,
        PDO::ATTR_STRINGIFY_FETCHES => false,
    ];
}

以上代码配合注释应该可以理解了所以不多解释了,直接进入函数

  • buildConnectString - 就是生成DSN连接串, 非常直白

      protected function buildConnectString() {
          return "$this->driver:host=$this->host;dbname=".self::$db;
      }
          // "mysql:host=localhost;dbname=sakila;"
  • connect - 连接数据库

      public function connect() {
          try {
              // 连接数据库,生成pdo实例, 将之赋予$connection,并存入$container之中
              self::$container[self::$db] = $this->connection = new PDO($this->buildConnectString(), $this->username, $this->password, $this->options);
              // 返回数据库连接
              return $this->connection;
          } catch (Exception $e) {
              // 若是失败, 返回原因
              // 还记得dd()吗?这个辅助函数还会一直用上
              dd($e->getMessage());
          }
      }
  • setDatabase - 切换数据库

      public function setDatabase($db) {
          self::$db = $db;
          return $this;
      }
  • _construct - 生成实例后第一步要干嘛

      function construct() {
          // 如果从未连接过该数据库, 那就新建连接
          if(empty(self::$container[self::$db])) $this->connect();
          // 反之, 从$container中提取, 无需再次通信
          $this->connection = self::$container[self::$db];
      }

接下来两个函数式配合着用的,单看可能会懵逼, 配合例子单步调试

$a = new Connector();

$bindValues = [
    'PENELOPE',
    'GUINESS'
];

dd($a->read('select * from actor where first_name = ? and last_name = ?', $bindValues));

返回值

array (size=1)
  0 => 
    object(stdClass)[4]
      public 'actor_id' => string '1' (length=1)
      public 'first_name' => string 'PENELOPE' (length=8)
      public 'last_name' => string 'GUINESS' (length=7)
      public 'last_update' => string '2006-02-15 04:34:33' (length=19)
  • read - 读取数据

      public function read($sql, $bindings) {
          // 将sql语句放入预处理函数
          // $sql = select * from actor where first_name = ? and last_name = ?
          $statement = $this->connection->prepare($sql);
          // 将附带参数带入pdo实例
          // $bindings = ['PENELOPE', 'GUINESS']
          $this->bindValues($statement, $bindings);
          // 执行
          $statement->execute();
          // 返回所有合法数据, 以Object对象为数据类型
          return $statement->fetchAll(PDO::FETCH_OBJ);
      }
  • bindValues - 将附带参数带入pdo实例

          // 从例子中可以看出, 我用在预处理的变量为?, 这是因为pdo的局限性, 有兴趣可以在评论区讨论这个问题
      public function bindValues($statement, $bindings) {
          // $bindings = ['PENELOPE', 'GUINESS']
          // 依次循环每一个参数
          foreach ($bindings as $key => $value) {
              // $key = 0/1
              // $value = 'PENELOPE'/'GUINESS'
              $statement->bindValue(
                  // 如果是字符串类型, 那就直接使用, 反之是数字, 将其+1
                  // 这里是数值, 因此返回1/2
                  is_string($key) ? $key : $key + 1,
                  // 直接放入值
                  // 'PENELOPE'/'GUINESS'
                  $value,
                  // 这里直白不多说
                  // PDO::PARAM_STR/PDO::PARAM_STR
                  is_int($value) ? PDO::PARAM_INT : PDO::PARAM_STR
              );
          }
      }

    所以懂了吗_( :3」∠)

    MVM mall 网上购物系统
    MVM mall 网上购物系统

    采用 php+mysql 数据库方式运行的强大网上商店系统,执行效率高速度快,支持多语言,模板和代码分离,轻松创建属于自己的个性化用户界面 v3.5更新: 1).进一步静态化了活动商品. 2).提供了一些重要UFT-8转换文件 3).修复了除了网银在线支付其它支付显示错误的问题. 4).修改了LOGO广告管理,增加LOGO链接后主页LOGO路径错误的问题 5).修改了公告无法发布的问题,可能是打压

    下载
  • update - 改写数据

      // 与read不同的地方在于, read返回数据, update返回boolean(true/false)
      public function update($sql, $bindings) {
          $statement = $this->connection->prepare($sql);
          $this->bindValues($statement, $bindings);
          return $statement->execute();
      }
  • delete - 删除数据

    // 与update一样, 分开是因为方便日后维护制定
      public function delete($sql, $bindings) {
          $statement = $this->connection->prepare($sql);
          $this->bindValues($statement, $bindings);
          return $statement->execute();
      }
  • create - 增加数据

    // 返回最新的自增ID, 如果有
      public function create($sql, $bindings) {
          $statement = $this->connection->prepare($sql);
          $this->bindValues($statement, $bindings);
          $statement->execute();
          return $this->lastInsertId();
      }
  • lastInsertId - 返回新增id, 如果有

      // pdo自带,只是稍微封装
      public function lastInsertId() {
          $id = $this->connection->lastInsertId();
          return empty($id) ? null : $id;
      }

过于高级复杂的SQL语句可能无法封装, 因此准备了可直接用RAW query通信数据库的两个函数

  • exec - 适用于增删改

      public function exec($sql) {
          return $this->connection->exec($sql);
      }
  • query - 适用于读

      public function query($sql) {
          $q = $this->connection->query($sql);
          return $q->fetchAll(PDO::FETCH_OBJ);
      }

将数据库事务相关的函数封装起来, 直白所以没有注释

        public function beginTransaction() {
        $this->connection->beginTransaction();
        return $this;
    }

    public function rollBack() {
        $this->connection->rollBack();
        return $this;
    }

    public function commit() {
        $this->connection->commit();
        return $this;
    }

    public function inTransaction() {
        return $this->connection->inTransaction();
    }

完整代码

 PDO::CASE_NATURAL,
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL,
        PDO::ATTR_STRINGIFY_FETCHES => false,
    ];

    function construct() {
        // 如果从未连接过该数据库, 那就新建连接
        if(empty(self::$container[self::$db])) $this->connect();
        // 反之, 从$container中提取, 无需再次通信
        $this->connection = self::$container[self::$db];
    }

    // 生成DSN连接串
    protected function buildConnectString() {
        return "$this->driver:host=$this->host;dbname=".self::$db;
    }

    // 连接数据库
    public function connect() {
        try {
            // 连接数据库,生成pdo实例, 将之赋予$connection,并存入$container之中
            self::$container[self::$db] = $this->connection = new PDO($this->buildConnectString(), $this->username, $this->password, $this->options);

            // 返回数据库连接
            return $this->connection;
        } catch (Exception $e) {
            // 若是失败, 返回原因
            dd($e->getMessage());
        }
    }

    // 切换数据库
    public function setDatabase($db) {
        self::$db = $db;
        return $this;
    }

    // 读取数据
    public function read($sql, $bindings) {
        // 将sql语句放入预处理函数
        $statement = $this->connection->prepare($sql);
        // 将附带参数带入pdo实例
        $this->bindValues($statement, $bindings);
        // 执行
        $statement->execute();
        // 返回所有合法数据, 以Object对象为数据类型
        return $statement->fetchAll(PDO::FETCH_OBJ);
    }

    // 将附带参数带入pdo实例
    public function bindValues($statement, $bindings) {
        // 依次循环每一个参数
        foreach ($bindings as $key => $value) {
            $statement->bindValue(
                // 如果是字符串类型, 那就直接使用, 反之是数字, 将其+1
                is_string($key) ? $key : $key + 1,
                // 直接放入值
                $value,
                // 这里直白不多说
                is_int($value) ? PDO::PARAM_INT : PDO::PARAM_STR
            );
        }
    }

    // 改写数据
    public function update($sql, $bindings) {
        // 与read不同的地方在于, read返回数据, update返回boolean(true/false)
        $statement = $this->connection->prepare($sql);
        $this->bindValues($statement, $bindings);
        return $statement->execute();
    }

    // 删除数据
    public function delete($sql, $bindings) {
        $statement = $this->connection->prepare($sql);
        $this->bindValues($statement, $bindings);
        return $statement->execute();
    }

    // 增加数据
    public function create($sql, $bindings) {
        $statement = $this->connection->prepare($sql);
        $this->bindValues($statement, $bindings);
        $statement->execute();
        return $this->lastInsertId();
    }

    // 返回新增id, 如果有
    public function lastInsertId() {
        $id = $this->connection->lastInsertId();
        return empty($id) ? null : $id;
    }

    // 适用于增删改
    public function exec($sql) {
        return $this->connection->exec($sql);
    }

    // 适用于读
    public function query($sql) {
        $q = $this->connection->query($sql);
        return $q->fetchAll(PDO::FETCH_OBJ);
    }

    public function beginTransaction() {
        $this->connection->beginTransaction();
        return $this;
    }

    public function rollBack() {
        $this->connection->rollBack();
        return $this;
    }

    public function commit() {
        $this->connection->commit();
        return $this;
    }

    public function inTransaction() {
        return $this->connection->inTransaction();
    }
}

本期疑问

1.) 因为php本身的特性, 默认情况下运行完所有代码类会自行析构,pdo自动断联, 所以我没有disconnect(),让pdo断开连接, 不知这样是不是一种 bad practice?
2.) 增加和改写数据的两个函数并不支持多组数据一次性加入,只能单次增该, 原因是我之前写了嫌太繁琐所以删了, 有心的童鞋可以提供方案

相关专题

更多
微信聊天记录删除恢复导出教程汇总
微信聊天记录删除恢复导出教程汇总

本专题整合了微信聊天记录相关教程大全,阅读专题下面的文章了解更多详细内容。

36

2026.01.18

高德地图升级方法汇总
高德地图升级方法汇总

本专题整合了高德地图升级相关教程,阅读专题下面的文章了解更多详细内容。

99

2026.01.16

全民K歌得高分教程大全
全民K歌得高分教程大全

本专题整合了全民K歌得高分技巧汇总,阅读专题下面的文章了解更多详细内容。

148

2026.01.16

C++ 单元测试与代码质量保障
C++ 单元测试与代码质量保障

本专题系统讲解 C++ 在单元测试与代码质量保障方面的实战方法,包括测试驱动开发理念、Google Test/Google Mock 的使用、测试用例设计、边界条件验证、持续集成中的自动化测试流程,以及常见代码质量问题的发现与修复。通过工程化示例,帮助开发者建立 可测试、可维护、高质量的 C++ 项目体系。

57

2026.01.16

java数据库连接教程大全
java数据库连接教程大全

本专题整合了java数据库连接相关教程,阅读专题下面的文章了解更多详细内容。

40

2026.01.15

Java音频处理教程汇总
Java音频处理教程汇总

本专题整合了java音频处理教程大全,阅读专题下面的文章了解更多详细内容。

19

2026.01.15

windows查看wifi密码教程大全
windows查看wifi密码教程大全

本专题整合了windows查看wifi密码教程大全,阅读专题下面的文章了解更多详细内容。

107

2026.01.15

浏览器缓存清理方法汇总
浏览器缓存清理方法汇总

本专题整合了浏览器缓存清理教程汇总,阅读专题下面的文章了解更多详细内容。

44

2026.01.15

ps图片相关教程汇总
ps图片相关教程汇总

本专题整合了ps图片设置相关教程合集,阅读专题下面的文章了解更多详细内容。

12

2026.01.15

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
PostgreSQL 教程
PostgreSQL 教程

共48课时 | 7.4万人学习

Django 教程
Django 教程

共28课时 | 3.2万人学习

MySQL 教程
MySQL 教程

共48课时 | 1.8万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

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