PHP實(shí)現(xiàn)的數(shù)據(jù)對(duì)象映射模式詳解
本文實(shí)例講述了PHP實(shí)現(xiàn)的數(shù)據(jù)對(duì)象映射模式。分享給大家供大家參考,具體如下:
還是代碼說話:這里還是遵循策略模式的psr-0代碼規(guī)范
數(shù)據(jù)表:
數(shù)據(jù)庫(kù)連接文件Db.php(如果沒有可以到前面一篇《PHP單例模式數(shù)據(jù)庫(kù)連接類與頁(yè)面靜態(tài)化》里面找)
自動(dòng)加載類文件Config.php(如果沒有可以去上一篇《PHP策略模式》里拿過來)
入口文件DataUser.php
<?php define('BASEDIR', __DIR__); //自動(dòng)加載在本文件中沒有被定義的類 require 'Config.php'; spl_autoload_register('Config::autolad'); //獲取數(shù)據(jù) $user = new Data(1); var_dump($user->id, $user->name, $user->money); //如果想要修改數(shù)據(jù) $user->id = 1; $user->name = 'zhangjianping'; $user->money = 10000; ?>
獲取數(shù)據(jù)的文件Data.php
<?php class Data { //數(shù)據(jù)項(xiàng) public $id; public $name; public $money; //數(shù)據(jù)庫(kù)連接對(duì)象 protected $con; //查詢數(shù)據(jù)的構(gòu)造函數(shù) public function __construct($id) { //連接數(shù)據(jù)庫(kù) $this->con = DB::getInstance()->connect(); //查詢數(shù)據(jù) $res = $this->con->query('select * from account where id = '.$id.' limit 1'); $data = $res->fetch(PDO::FETCH_ASSOC); //把取出來的數(shù)據(jù)項(xiàng)存儲(chǔ)起來 $this->id = $data['id']; $this->name = $data['name']; $this->money = $data['money']; } //修改數(shù)據(jù)的析構(gòu)函數(shù) public function __destruct() { $this->con->query("update account set name = '{$this->name}', 'money = {$this->money}' where id = {$this->id}"); } } ?>
下面我們就使用工廠模式,注冊(cè)樹模式,數(shù)據(jù)對(duì)象映射模式來完善一下這個(gè)例子
- 數(shù)據(jù)庫(kù)連接文件Db.php
- 自動(dòng)加載類文件Config.php
- 獲取數(shù)據(jù)的文件Data.php
我們將原來的入口文件改一下:
DataUser.php
<?php define('BASEDIR', __DIR__); require 'Config.php'; spl_autoload_register(Config::autoload); class DataUser { public function index() { //使用工廠模式來生成對(duì)象 $user = Factory::getUser(1); var_dump($user->id); $this->name(); $this->money(); } public function name() { $user = Factory::getUser(1); var_dump($user->name); } public function money() { $user = Factory::getUser(1); var_dump($user->money); } } ?>
工廠類Factory.php
<?php class Factory { static function getUser($id) { //這里使用注冊(cè)器模式,不然的話,在上面的文件中,使用工廠模式生成對(duì)象得時(shí)候就會(huì)多次創(chuàng)建對(duì)象,很占用資源 //根據(jù)id不同插入到注冊(cè)樹對(duì)象中 $key = 'user_'.$id; //從注冊(cè)器中取出對(duì)象 $user = Register::get($key); //如果注冊(cè)器中沒有就創(chuàng)建一個(gè)對(duì)象并注冊(cè)上去 if(!isset($user)) { $user = new Data($id); $user = Register::set($key, $user); } return $user; } } ?>
注冊(cè)器類Register.php
<?php class Register { //存儲(chǔ)對(duì)象得變量 protected static $object; //注冊(cè)入注冊(cè)器 public static function set($key, $value) { self::$object[$key] = $value; } //從注冊(cè)器中取出 public static function get($key) { return self::$object[$key]; } //從注冊(cè)器中刪除 public static function _unset($key) { unset(self::$object[$key]); } } ?>
如果這時(shí)候我們將Data.php修改為Data1.php,那么在不使用工廠模式的時(shí)候就要一個(gè)一個(gè)的去修改類名,現(xiàn)在只需要在工廠模式中修改一下就好了,我們也可以打印出每一個(gè)對(duì)象,這時(shí)候我們會(huì)發(fā)現(xiàn)這3個(gè)對(duì)象都是一樣的,這是因?yàn)槲覀兪褂昧俗?cè)器模式。
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- PHP中數(shù)據(jù)庫(kù)單例模式的實(shí)現(xiàn)代碼分享
- php設(shè)計(jì)模式 DAO(數(shù)據(jù)訪問對(duì)象模式)
- 淺析php設(shè)計(jì)模式之?dāng)?shù)據(jù)對(duì)象映射模式
- PHP基于單例模式實(shí)現(xiàn)的數(shù)據(jù)庫(kù)操作基類
- PHP的中使用非緩沖模式查詢數(shù)據(jù)庫(kù)的方法
- PHP單例模式應(yīng)用示例【多次連接數(shù)據(jù)庫(kù)只實(shí)例化一次】
- PHP單例模式數(shù)據(jù)庫(kù)連接類與頁(yè)面靜態(tài)化實(shí)現(xiàn)方法
- PHP數(shù)據(jù)對(duì)象映射模式實(shí)例分析
- PHP設(shè)計(jì)模式之?dāng)?shù)據(jù)訪問對(duì)象模式(DAO)原理與用法實(shí)例分析
- PHP數(shù)據(jù)源架構(gòu)模式之表入口模式實(shí)例分析
相關(guān)文章
phpmailer發(fā)送gmail郵件實(shí)例詳解
本篇文章是對(duì)phpmailer發(fā)送gmail郵件實(shí)例進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06PHP基于關(guān)聯(lián)數(shù)組20行代碼搞定約瑟夫問題示例
這篇文章主要介紹了PHP基于關(guān)聯(lián)數(shù)組20行代碼搞定約瑟夫問題,結(jié)合具體實(shí)例分析了php使用關(guān)聯(lián)數(shù)組解決約瑟夫問題的相關(guān)操作技巧,需要的朋友可以參考下2017-11-11ASP和PHP實(shí)現(xiàn)生成網(wǎng)站快捷方式并下載到桌面的方法
這篇文章主要介紹了PHP實(shí)現(xiàn)生成網(wǎng)站快捷方式并下載到桌面的方法,比加入收藏、設(shè)為首頁(yè)更給力哦,需要的朋友可以參考下2014-05-05利用PHP實(shí)現(xiàn)短域名互轉(zhuǎn)
如何使用PHP實(shí)現(xiàn)短域名互轉(zhuǎn)?下面的代碼可以幫助你實(shí)現(xiàn),非常簡(jiǎn)單,需要的朋友可以參考下2013-07-07簡(jiǎn)單的php寫入數(shù)據(jù)庫(kù)類代碼分享
簡(jiǎn)單的php寫入數(shù)據(jù)庫(kù)類代碼分享,學(xué)習(xí)php的朋友可以參考下。2011-07-07PHP利用REFERER根居訪問來地址進(jìn)行頁(yè)面跳轉(zhuǎn)
這篇文章主要是根據(jù)referfer判斷來源,然后跳轉(zhuǎn)到制定頁(yè)面,以防出現(xiàn)了無法訪問頁(yè)面,用戶體驗(yàn)不好2013-09-09