1. 写一个抽象类并继承它, 内容自定
2. 模仿课堂案例,写一个接口实现CURD操作, 并扩展一到二个方法
在实例中:
【1】接口iDbPlug中定义了数据库的创建、CURD 共5个方法,实现中改为链式调用的方法;
【2】接口iDbParam中定义了数据库连接参数;
【3】抽象类DbSql中 实现接口 iDbPlug的5个方法,自身定义了7个和SQL语句有关的方法,还有一个设置PDO运行属性的抽象方法;
【4】类DBSet,继承了抽象类DbSql,实现了接口iDbParam,在这个类里采用单例模式,实现数据库的自动连接;
【5】类DB中触发数据库连接,通过__callStatic()实现SQL语句的拼接,链式调用并执行CURD;
实例中遗留问题不少,不过基本完成了所有方法,可以实现绝大多数的SQL语句拼接,和数据库操作。
运行测试情况:
实例代码
<?php namespace _1009; //定义一个数据库增删改查+创建的接口 interface iDbPlug { public function create($fields); public function insert($data); public function update($data); public function query(); public function delete(); } //定义一个包含数据库连接参数的接口 interface iDbParam { const TYPE = 'mysql'; const HOST = '127.0.0.1'; const DBNAME = 'test'; const USER_NAME = '*'; const PASSWORD = '*'; } //抽象类包含公共的方法、包括抽象的需要实现的方法,实现对应接口的方法 abstract class DbSql implements iDbPlug { protected $pdo = null; // protected static $pdo = null; protected $table; private $field = '*'; private $where; private $limit; private $like; private $in; private $orderBy; //SQL 语句关键词处理 public function table($tableName) { $this->table = $tableName; return $this; } public function field($fields) { $this->field = empty($fields) ? '*' : $fields; return $this; } public function where($where) { $this->where = empty($where) ? $where : ' WHERE ' . $where; return $this; } public function like($like) { $this->like = empty($like) ? $like : ' LIKE ' . "'" . $like . "'"; return $this; } public function limit($limit) { $this->limit = empty($limit) ? $limit : ' LIMIT ' . $limit; return $this; } public function in($in) { $this->in = empty($in) ? $in : ' IN (' . $in . ')'; return $this; } public function orderBy($orderBy, $option = 'ASC') { $sort = in_array($option, ['DESC', 'ASC']) ? $option : 'ASC'; $this->orderBy = empty($orderBy) ? $orderBy : ' ORDER BY ' . $orderBy . ' ' . $sort; return $this; } //----------------以下数据库的增删改查,创建方法--------------------------- public function create($fields) { // TODO...创建新表 } public function insert($data) { $fields = ''; $value = ''; foreach ($data as $key => $v) { $fields = $fields . ',' . $key; $value = $value . ',:' . $key; } $fields = '(' . ltrim($fields, ',') . ')'; $value = '(' . ltrim($value, ',') . ')'; $sql = 'INSERT INTO ' . $this->table . ' ' . $fields . ' VALUES ' . $value; $stmt = $this->pdo->prepare($sql); $stmt->execute($data); return [ 'count' => $stmt->rowCount(), 'id' => $this->pdo->lastInsertId(), ]; } public function update($data) { $keyArr = array_keys($data); $set = ''; foreach ($keyArr as $value) { $set .= $value . '=:' . $value . ','; } $set = rtrim($set, ','); $sql = 'UPDATE ' . $this->table . ' SET ' . $set . $this->where; $stmt = $this->pdo->prepare($sql); $stmt->execute($data); return $stmt->rowCount(); } public function query() { $sql = 'SELECT ' . $this->field . ' FROM ' . $this->table . $this->where . $this->like . $this->in . $this->orderBy . $this->limit; $stmt = $this->pdo->prepare($sql); $stmt->execute(); return $stmt->fetchAll(\PDO::FETCH_ASSOC); } public function delete() { $sql = 'DELETE FROM ' . $this->table . $this->where . $this->orderBy . $this->limit; $stmt = $this->pdo->prepare($sql); $stmt->execute(); return $stmt->rowCount(); } abstract public function setPDOattr($attr, $value); } class DBSet extends DbSql implements iDbParam { //TODO ---需要改造 private static $type = iDbParam::TYPE; private static $host = iDbParam::HOST; private static $dbname = iDbParam::DBNAME; private static $userName = iDbParam::USER_NAME; private static $password = iDbParam::PASSWORD; private static $instance = null; protected static $pdo = null; private function __construct() { $dsn = self::$type . ':host=' . self::$host . ';dbname=' . self::$dbname; $user = self::$userName; $password = self::$password; } private function __clone() { } public static function getInstance() { if (is_null(self::$instance)) { self::$instance = new self(); self::$pdo = new \PDO($dsn, $user, $password); } return self::$instance; } public function setPDOattr($attr, $value) { $this->pdo->setAttribute($attr, $value); //PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ } } class DB { protected static $query = null; public static function connection() { self::$query = DBSet::getInstance(); } public static function __callStatic($name, $argus) { return call_user_func_array([self::$query, $name], $argus); } } DB::connection(); //查询 // $staffs = DB::table('staff') // ->field('staff_id,name,position,mobile') // ->where('name = "令狐冲"') // ->query(); // foreach ($staffs as $value) { // print_r($value); // echo '<br>'; // } /////////////////////////////////////////// //添加 $data = [ 'name' => 'xxx', 'age' => 100, 'sex' => 1, 'position' => '老板', 'mobile' => '12349876523', 'hiredate' => time(), ]; $res = DB::table('staff')->insert($data); echo '成功的新增了: ' . $res['count'] . ' 条记录,新增的记录的主键ID是: ' . $res['id']; echo '<hr>'; ////////////////////////////////////////////////// // 更新 // $data = [ // 'name' => '依琳', // 'age' => 20, // 'sex' => 0, // 'position' => '小尼姑', // ]; // $where = 'staff_id = 17'; // $staffs = DB::table('staff')->where($where)->update($data); // echo '成功的更新了: ' . $staffs . ' 条记录'; ////////////////////////////////////////////////// //删除 $where = 'name = "xxx"'; $staffs = DB::table('staff')->where($where)->delete(); echo '成功的删除了: ' . $staffs . ' 条记录'; echo '<hr>'; ?> 运行实例 » 点击 "运行实例" 按钮查看在线实例