老生常談PHP面向?qū)ο笾畼?biāo)識(shí)映射
標(biāo)識(shí)映射在數(shù)據(jù)映射器的基礎(chǔ)上增加了標(biāo)識(shí)映射類,主要功能是保存已經(jīng)創(chuàng)建好的對(duì)象,在需要的時(shí)候可以直接獲取而不是重復(fù)創(chuàng)建造成系統(tǒng)性能的下降。
在數(shù)據(jù)映射器基礎(chǔ)上還增加了部分調(diào)用標(biāo)識(shí)映射類的方法,示例代碼如下:
namespace woo\domain; //標(biāo)識(shí)映射類 class ObjectWatcher{ private $all = array(); //存放對(duì)象的小倉(cāng)庫(kù) private static $instance; //單例 private function __construct (){} static function instance(){ if(!self::$instance){ self::$instance = new ObjectWatcher(); } return self::$instance; } //獲取一個(gè)唯一的標(biāo)識(shí),這里采用了領(lǐng)域類類名+ID的方式創(chuàng)建一個(gè)唯一標(biāo)識(shí),避免多個(gè)數(shù)據(jù)庫(kù)表調(diào)用這個(gè)類時(shí)出現(xiàn)ID重復(fù)的問(wèn)題 function globalKey(DomainObject $obj){ $key = get_class($obj) . "." . $obj->getId(); return $key; } //添加對(duì)象 static function add(DomainObject $obj){ $inst = self::instance(); $inst->all[$inst->globalKey($obj)] = $obj; } //獲取對(duì)象 static function exists($classname,$id){ $inst = self::instance(); $key = "$classname.$id"; if(isset($inst->all[$key]){ return $inst->all[$key]; } return null; } } namespace woo\mapper; abstract class Mapper{ //抽象基類 abstract static $PDO; //操作數(shù)據(jù)庫(kù)的pdo對(duì)象 function __construct (){ if(!isset(self::$PDO){ $dsn = \woo\base\ApplicationRegistry::getDSN(); if(is_null($dsn)){ throw new \woo\base\AppException("no dns"); } self::$PDO = new \PDO($dsn); self::$PDO->setAttribute(\PDO::ATTR_ERRMODE,\PDO::ERRMODE_EXCEPTION); } } //數(shù)據(jù)映射器基礎(chǔ)上新增的方法以下會(huì)簡(jiǎn)稱新增,這里的作用的是獲取對(duì)象而不是查詢數(shù)據(jù)庫(kù)并重復(fù)創(chuàng)建對(duì)象 //(對(duì)比一下原數(shù)據(jù)映射器的相關(guān)代碼即可了解) private function getFroMap($id){ return \woo\domain\ObjectWatcher::exists($this->targetClass(),$id); } //新增,這里的作用的是將創(chuàng)建的對(duì)象保存起來(lái) private function addToMap(\woo\domain\DomainObject $obj){////// return \woo\domain\ObjectWatcher::add($obj); } //對(duì)比原數(shù)據(jù)映射器的代碼,便發(fā)現(xiàn)它不是直接創(chuàng)建對(duì)象而是首先在標(biāo)識(shí)映射類中查找,找不到才調(diào)用的 //子類的方法創(chuàng)建并插入到標(biāo)識(shí)映射類,下面的find方法也遵循了這一原則 function createObject($array){ $old = $this->getFromMap($array['id']); //新增 if($old){return $old} //新增 $obj = $this->doCreateObject($array); //在子類中實(shí)現(xiàn) $this->addToMap($obj); //新增 return $obj; } // function find($id){ //通過(guò)ID從數(shù)據(jù)庫(kù)中獲取一條數(shù)據(jù)并創(chuàng)建為對(duì)象 $old = $this->getFromMap($id); //新增 if($old){return $old} //新增 $this->selectStmt()->execute(array($id)); $array= $this->selectStmt()->fetch(); $this->selectStmt()->closeCursor(); if(!is_array($array)){ return null; } if(!isset($array['id'])){ return null; } $object = $this->createObject($array); $this->addToMap($object); //新增 return $object; } function insert(\woo\domain\DomainObject $obj){ //將對(duì)象數(shù)據(jù)插入數(shù)據(jù)庫(kù) $this->doInsert($obj); $this->addToMap($obj); //新增 } //需要在子類中實(shí)現(xiàn)的各抽象方法 abstract function targetClass();////// abstract function update(\woo\domain\DomainObject $objet); protected abstract function doCreateObject(array $array); protected abstract function selectStmt(); protected abstract function doInsert(\woo\domain\DomainObject $object); } class SpaceMapper extends Mapper { //其他代碼在數(shù)據(jù)映射器一文中已有實(shí)現(xiàn)這里略過(guò) //............. //類名,在標(biāo)識(shí)映射類中生成唯一標(biāo)識(shí)用的 protected function targetClass(){ return "woo\\domain\\Space"; } }
以上這篇老生常談PHP面向?qū)ο笾畼?biāo)識(shí)映射就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- 詳解PHP的Laravel框架中Eloquent對(duì)象關(guān)系映射使用
- ThinkPHP中公共函數(shù)路徑和配置項(xiàng)路徑的映射分析
- 回答PHPCHINA上的幾個(gè)問(wèn)題:URL映射
- 解密ThinkPHP3.1.2版本之模塊和操作映射
- PHP實(shí)現(xiàn)路由映射到指定控制器
- 淺析php設(shè)計(jì)模式之?dāng)?shù)據(jù)對(duì)象映射模式
- PHP面向?qū)ο笾I(lǐng)域模型+數(shù)據(jù)映射器實(shí)例(分析)
- PHP實(shí)現(xiàn)的數(shù)據(jù)對(duì)象映射模式詳解
- PHP數(shù)據(jù)對(duì)象映射模式實(shí)例分析
- php實(shí)現(xiàn)映射操作實(shí)例詳解
相關(guān)文章
PHP統(tǒng)計(jì)當(dāng)前在線用戶數(shù)實(shí)例講解
我們要統(tǒng)計(jì)在一段時(shí)間內(nèi)訪問(wèn)站點(diǎn)的人數(shù),有多種解決方案,你可以使用cookie,session結(jié)合文本或者數(shù)據(jù)庫(kù)來(lái)記錄用戶訪問(wèn)數(shù)。本文將使用PHP,結(jié)合Mysql以及jQuery,展示一個(gè)統(tǒng)計(jì)在線人數(shù)以及訪客地區(qū)分布的示例。2015-10-10微信公眾號(hào)OAuth2.0網(wǎng)頁(yè)授權(quán)問(wèn)題淺析
根據(jù)需求,我今天完成的是微信的網(wǎng)頁(yè)授權(quán)然后拉取用戶的一些基本信息的問(wèn)題。具體內(nèi)容詳情大家通過(guò)本文學(xué)習(xí)吧2017-01-01Thinkphp5 如何隱藏入口文件index.php(URL重寫(xiě))
今天小編就為大家分享一篇Thinkphp5 如何隱藏入口文件index.php(URL重寫(xiě)),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10Laravel框架實(shí)現(xiàn)的記錄SQL日志功能示例
這篇文章主要介紹了Laravel框架實(shí)現(xiàn)的記錄SQL日志功能,結(jié)合實(shí)例形式總結(jié)分析了Laravel框架監(jiān)聽(tīng)并記錄SQL相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2018-06-06laravel按天、按小時(shí),查詢數(shù)據(jù)的實(shí)例
今天小編就為大家分享一篇laravel按天、按小時(shí),查詢數(shù)據(jù)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10php版微信開(kāi)發(fā)之接收消息,自動(dòng)判斷及回復(fù)相應(yīng)消息的方法
這篇文章主要介紹了php版微信開(kāi)發(fā)之接收消息,自動(dòng)判斷及回復(fù)相應(yīng)消息的方法,涉及基于微信消息處理接口的調(diào)用相關(guān)操作技巧,需要的朋友可以參考下2016-09-09ubutu 16.04環(huán)境下,PHP與mysql數(shù)據(jù)庫(kù),網(wǎng)頁(yè)登錄驗(yàn)證實(shí)例講解
下面小編就為大家?guī)?lái)一篇ubutu 16.04環(huán)境下,PHP與mysql數(shù)據(jù)庫(kù),網(wǎng)頁(yè)登錄驗(yàn)證實(shí)例講解。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07