批改狀態(tài):未批改
老師批語(yǔ):
通過(guò)靜態(tài)魔術(shù)方法進(jìn)行跳轉(zhuǎn),實(shí)現(xiàn)對(duì)象方法的跨類調(diào)用 __callStatic:
demo1.php代碼:
<?php //使用靜態(tài)方法的重載技術(shù), 實(shí)現(xiàn)一個(gè)數(shù)據(jù)庫(kù)訪問(wèn)類,并實(shí)例演示鏈接調(diào)用的實(shí)現(xiàn)過(guò)程 require 'Query1.php'; class Db { //創(chuàng)建一個(gè)私有的靜態(tài)屬性,用于數(shù)據(jù)庫(kù)連接 protected static $pdo = null; // 數(shù)據(jù)庫(kù)連接方法, 每次查詢時(shí)再連接, 實(shí)現(xiàn)真正的惰性連接,節(jié)省系統(tǒng)開(kāi)銷 public static function connection() { //連接數(shù)據(jù)的參數(shù) self::$pdo = new PDO('mysql:host=127.0.0.1;dbname=php','root','root'); } //查詢類操作入口,通過(guò)靜態(tài)魔術(shù)方法進(jìn)行跳轉(zhuǎn),實(shí)現(xiàn)對(duì)象方法的跨類調(diào)用 public static function __callStatic($name, $arguments) { //創(chuàng)建pdo對(duì)象,并連接數(shù)據(jù)庫(kù) self::connection(); //實(shí)例化查詢類,將連接對(duì)象作為參數(shù) $query = new Query(self::$pdo); //執(zhí)行查詢類Query中的對(duì)象方法,注意參數(shù)是數(shù)組,我只需要第一個(gè)參數(shù):表名, 所以加了索引鍵名 return call_user_func_array([$query,$name],[$arguments[0]]); } } //客戶端的鏈?zhǔn)秸{(diào)用 //以Db類做入整數(shù)數(shù)據(jù)庫(kù)操作的入口,SQL語(yǔ)句的各個(gè)部分用對(duì)象方法提供 //鏈?zhǔn)讲僮魇乾F(xiàn)代PHP框架的基礎(chǔ),非常有用 $cats = Db::table('category') ->field('cate_id, name ,alias') ->where('cate_id >= 2') ->limit('2') ->select(); //foreach循環(huán)查看獲取的數(shù)據(jù)庫(kù)數(shù)據(jù) foreach ($cats as $cat) { print_r($cat); echo '<br>'; } //$cats = Db::table('category') // ->field('kr,抗日神劇') // ->value('1') // ->update(); // //foreach ($cats as $cat) { // print_r($cat); // echo '<br>'; //}
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
Query.php代碼:
<?php //數(shù)據(jù)庫(kù)查詢類 class Query { //連接對(duì)象 public $pdo = null; //字段列表 public $field = ''; //數(shù)據(jù)表名 public $table = ''; //查詢條件 public $where = ''; //顯示數(shù)量 public $limit = ''; //修改值 public $value = ''; //構(gòu)造方法,初始化連接對(duì)象 public function __construct(PDO $pdo) { //連接對(duì)象是對(duì)象方法的共享屬性 $this->pdo = $pdo; } //調(diào)用表名 public function table($tableName) { $this->table = $tableName; //返回當(dāng)前對(duì)象,便于鏈?zhǔn)秸{(diào)用該對(duì)象的其他方法 return $this; } //設(shè)置查詢字段 public function field($fields) { $this->field = $fields; return $this; } //設(shè)置查詢條件 public function where($where) { $this->where = $where; return $this; } //設(shè)置顯示數(shù)量 public function limit($limit) { $this->limit = $limit; return $this; } //設(shè)置值 public function value($value) { $this->limit = $value; return $this; } //創(chuàng)建SQL查詢語(yǔ)句對(duì)象,并返回查詢結(jié)果 public function select() { //查詢條件設(shè)置,確保鏈?zhǔn)椒椒í?dú)立 $fields = empty($this->field) ? '*' : $this->field; $where = empty($this->where) ? '' : ' WHERE ' .$this->where; $limit = empty($this->limit) ? '' : ' LIMIT ' . $this->limit; //SQL查詢語(yǔ)句 $sql = 'SELECT '.$fields.' FROM '.$this->table . $where . $limit; //預(yù)處理查詢 $stmt = $this->pdo->prepare($sql); $stmt->execute(); return $stmt->fetchAll(PDO::FETCH_ASSOC); } // //創(chuàng)建SQL修改語(yǔ)句對(duì)象,并返回修改結(jié)果結(jié)果 // public function update() // { // //查詢條件設(shè)置,確保鏈?zhǔn)椒椒í?dú)立 // $fields = empty($this->field) ? '*' : $this->field; // $where = empty($this->where) ? '' : ' WHERE ' .$this->where; // $value = empty($this->value) ? '' : '='.$this->value; // // //SQL查詢語(yǔ)句 // $sql = 'UPDATE '.$this->table.' SET '.$fields.$value. $where; // // //預(yù)處理查詢 // $stmt = $this->pdo->prepare($sql); // $stmt->execute(); // return $stmt->fetch(PDO::FETCH_ASSOC); // } }
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
最終在頁(yè)面上打印出的數(shù)據(jù)庫(kù)查詢結(jié)果顯示:
微信掃碼
關(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)