批改狀態(tài):合格
老師批語(yǔ):總結(jié)的非常好, 接口才是以后開(kāi)發(fā)最常用的, 抽象類只是接口的一個(gè)實(shí)例罷了
類的自動(dòng)加載
str_replace() 字符串替換語(yǔ)法
sql_autoload_register( function(){} ) 自動(dòng)加載函數(shù)
file_exists() 確定文件是否存在
抽象類與抽象方法:
abstract 抽象關(guān)鍵字;
抽象類不能實(shí)例化,類中抽象方法必須在子類全部實(shí)現(xiàn);
abstract public function setName ($value); 抽象方法
在子類 創(chuàng)建public function setName($value); 實(shí)現(xiàn)父類的抽象方法
接口原理與實(shí)現(xiàn)
interface {} 定義一個(gè)接口;
implement 類實(shí)現(xiàn)接口的關(guān)鍵字
注意:
1.接口不能實(shí)例化;
2.接口中只允許出現(xiàn)抽象方法;
3.接口中成員必須是公共/public;
4.允許有常量;
5.實(shí)現(xiàn)接口的子類,必須將接口的抽象方法全部實(shí)現(xiàn);
類的自動(dòng)加載
class Loader { public static function autoLoader() { sql_autoload_register(function($className){ $path = str_replace('\\','/',$className); $path = __DIR__ . '/' . $path . 'php'; if(file_exists($path)){ require $path ; } }); } }
<?php namespace demo; //定義一個(gè)接口 interface iCurd { //增加 public function create($data); //查詢 public function read(); //設(shè)置 public function update($data,$where); //刪除 public function delete($where); } class sub implements iCurd { //連接對(duì)象 protected $pdo=null; protected $table='staff'; //連接數(shù)據(jù)庫(kù) public function __construct($dsn,$username,$password,$table) { $this->pdo = new \PDO($dsn,$username,$password); $this->table = $table; } //增加方法 public function create($data) { //字段 $fields = ' (name,age,sex,position,mobile,hiredate) '; //值 $values = ' (:name, :age, :sex, :position, :mobile, :hiredate) '; //sql語(yǔ)句 $sql = 'INSERT INTO '.$this->table . $fields . ' VALUES ' . $values ; //sql預(yù)處理 $stmt = $this->pdo->prepare($sql); // die($stmt->debugDumpParams()); $stmt->execute($data); //返回新增數(shù)據(jù) return [ 'count' => $stmt->rowCount(), 'id' => $this->pdo->lastInsertId() ]; } //查詢方法 public function read($fields = '*',$where = '',$limit = '0, 5') { $where = empty($where) ? $where : ' WHERE '.$where; $limit = ' LIMIT '.$limit; $sql = 'SELECT '.$fields . ' FROM '.$this->table . $where . $limit; $stmt = $this->pdo->prepare($sql); $stmt -> execute(); // die($stmt->debugDumpParams()); return $stmt->fetchAll(\PDO::FETCH_ASSOC); } //更新方法 public function update($data,$where) { // 獲取$data的鍵值 $keyArr = array_keys($data); $set = ''; foreach ($keyArr as $value){ $set .= $value .'= :'.$value.','; } //去除右邊的逗號(hào) $set = rtrim($set,','); //sql語(yǔ)句 $sql = 'UPDATE ' .$this->table .' SET '.$set.' WHERE '.$where; //預(yù)處理 $stmt = $this->pdo->prepare($sql); $stmt->execute($data); // die($stmt->debugDumpParams()); return $stmt->rowCount(); } //刪除方法 public function delete($where) { $sql = 'DELETE FROM '.$this->table .' WHERE '. $where; $stmt = $this->pdo->prepare($sql); $stmt->execute(); return $stmt->rowCount(); } } //客戶端代碼 //實(shí)例化數(shù)據(jù)庫(kù) $dsn = 'mysql:host=127.0.0.1;dbname=php'; $password = 'root'; $username = 'root'; $table = 'staff'; //表名 $db = new sub($dsn,$username,$password,$table); //導(dǎo)入數(shù)據(jù) $data = [ 'name'=> '洪吉潮', 'age'=> 18, 'sex' => 1, 'position' => '主席', 'mobile'=> '15626475734', 'hiredate' => time() ]; //增加執(zhí)行結(jié)果 // $res = $db->create($data); // echo '成功新增 '. $res['count']. '條記錄, 新增記錄ID是: '. $res['id']; // echo '<hr>'; //查詢執(zhí)行結(jié)果 foreach ($db->read('name,age,position','staff_id>5','5')as $item ){ print_r($item); echo '<br>'; } echo '<hr>'; //更新執(zhí)行結(jié)果 $data = [ 'name' => '李文茜' ]; $where = 'staff_id=12'; echo '成功更新'.$db->update($data,$where).'條記錄'; echo '<hr>'; //刪除執(zhí)行結(jié)果 $where = 'staff_id=11'; echo '成功刪除'.$db->delete($where).'條記錄';
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號(hào)
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)