本文實(shí)例講述了PHP實(shí)現(xiàn)的數(shù)據(jù)對象映射模式。分享給大家供大家參考,具體如下:
還是代碼說話:這里還是遵循策略模式的psr-0代碼規(guī)范
數(shù)據(jù)表:
數(shù)據(jù)庫連接文件Db.php(如果沒有可以到前面一篇《PHP單例模式數(shù)據(jù)庫連接類與頁面靜態(tài)化》里面找)
自動加載類文件Config.php(如果沒有可以去上一篇《PHP策略模式》里拿過來)
入口文件DataUser.php
?php define('BASEDIR', __DIR__); //自動加載在本文件中沒有被定義的類 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ù)庫連接對象 protected $con; //查詢數(shù)據(jù)的構(gòu)造函數(shù) public function __construct($id) { //連接數(shù)據(jù)庫 $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)存儲起來 $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}"); } } ?>
下面我們就使用工廠模式,注冊樹模式,數(shù)據(jù)對象映射模式來完善一下這個(gè)例子
我們將原來的入口文件改一下:
DataUser.php
?php define('BASEDIR', __DIR__); require 'Config.php'; spl_autoload_register(Config::autoload); class DataUser { public function index() { //使用工廠模式來生成對象 $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) { //這里使用注冊器模式,不然的話,在上面的文件中,使用工廠模式生成對象得時(shí)候就會多次創(chuàng)建對象,很占用資源 //根據(jù)id不同插入到注冊樹對象中 $key = 'user_'.$id; //從注冊器中取出對象 $user = Register::get($key); //如果注冊器中沒有就創(chuàng)建一個(gè)對象并注冊上去 if(!isset($user)) { $user = new Data($id); $user = Register::set($key, $user); } return $user; } } ?>
注冊器類Register.php
?php class Register { //存儲對象得變量 protected static $object; //注冊入注冊器 public static function set($key, $value) { self::$object[$key] = $value; } //從注冊器中取出 public static function get($key) { return self::$object[$key]; } //從注冊器中刪除 public static function _unset($key) { unset(self::$object[$key]); } } ?>
如果這時(shí)候我們將Data.php修改為Data1.php,那么在不使用工廠模式的時(shí)候就要一個(gè)一個(gè)的去修改類名,現(xiàn)在只需要在工廠模式中修改一下就好了,我們也可以打印出每一個(gè)對象,這時(shí)候我們會發(fā)現(xiàn)這3個(gè)對象都是一樣的,這是因?yàn)槲覀兪褂昧俗云髂J健?/p>
更多關(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ù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計(jì)有所幫助。
標(biāo)簽:昭通 九江 十堰 涼山 遼陽 韶關(guān) 甘肅 梅河口
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《PHP實(shí)現(xiàn)的數(shù)據(jù)對象映射模式詳解》,本文關(guān)鍵詞 PHP,實(shí)現(xiàn),的,數(shù)據(jù),對象,映射,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。