老生常談PHP面向對象之標識映射
標識映射在數(shù)據(jù)映射器的基礎上增加了標識映射類,主要功能是保存已經(jīng)創(chuàng)建好的對象,在需要的時候可以直接獲取而不是重復創(chuàng)建造成系統(tǒng)性能的下降。
在數(shù)據(jù)映射器基礎上還增加了部分調用標識映射類的方法,示例代碼如下:
namespace woo\domain;
//標識映射類
class ObjectWatcher{
private $all = array(); //存放對象的小倉庫
private static $instance; //單例
private function __construct (){}
static function instance(){
if(!self::$instance){
self::$instance = new ObjectWatcher();
}
return self::$instance;
}
//獲取一個唯一的標識,這里采用了領域類類名+ID的方式創(chuàng)建一個唯一標識,避免多個數(shù)據(jù)庫表調用這個類時出現(xiàn)ID重復的問題
function globalKey(DomainObject $obj){
$key = get_class($obj) . "." . $obj->getId();
return $key;
}
//添加對象
static function add(DomainObject $obj){
$inst = self::instance();
$inst->all[$inst->globalKey($obj)] = $obj;
}
//獲取對象
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ù)庫的pdo對象
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ù)映射器基礎上新增的方法以下會簡稱新增,這里的作用的是獲取對象而不是查詢數(shù)據(jù)庫并重復創(chuàng)建對象
//(對比一下原數(shù)據(jù)映射器的相關代碼即可了解)
private function getFroMap($id){
return \woo\domain\ObjectWatcher::exists($this->targetClass(),$id);
}
//新增,這里的作用的是將創(chuàng)建的對象保存起來
private function addToMap(\woo\domain\DomainObject $obj){//////
return \woo\domain\ObjectWatcher::add($obj);
}
//對比原數(shù)據(jù)映射器的代碼,便發(fā)現(xiàn)它不是直接創(chuàng)建對象而是首先在標識映射類中查找,找不到才調用的
//子類的方法創(chuàng)建并插入到標識映射類,下面的find方法也遵循了這一原則
function createObject($array){
$old = $this->getFromMap($array['id']); //新增
if($old){return $old} //新增
$obj = $this->doCreateObject($array); //在子類中實現(xiàn)
$this->addToMap($obj); //新增
return $obj;
}
//
function find($id){ //通過ID從數(shù)據(jù)庫中獲取一條數(shù)據(jù)并創(chuàng)建為對象
$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){ //將對象數(shù)據(jù)插入數(shù)據(jù)庫
$this->doInsert($obj);
$this->addToMap($obj); //新增
}
//需要在子類中實現(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ù)映射器一文中已有實現(xiàn)這里略過
//.............
//類名,在標識映射類中生成唯一標識用的
protected function targetClass(){
return "woo\\domain\\Space";
}
}
以上這篇老生常談PHP面向對象之標識映射就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
thinkPHP5.1框架使用SemanticUI實現(xiàn)分頁功能示例
這篇文章主要介紹了thinkPHP5.1框架使用SemanticUI實現(xiàn)分頁功能,結合實例形式分析了SemanticUI擴展插件的定義及使用分頁相關操作技巧,需要的朋友可以參考下2019-08-08
PHP快速導出百萬級數(shù)據(jù)到CSV或者EXCEL文件
這篇文章主要介紹了PHP快速導出百萬級數(shù)據(jù)到CSV或者EXCEL文件,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-11-11
Windows下Apache + PHP SESSION丟失的解決過程全紀錄
這篇文章主要介紹了Windows下Apache + PHP SESSION丟失的解決過程全紀錄,花費了很長時間,最終解決的方式卻令人啼笑皆非,郁悶之極。2015-04-04
ThinkPHP使用getlist方法實現(xiàn)數(shù)據(jù)搜索功能示例
這篇文章主要介紹了ThinkPHP使用getlist方法實現(xiàn)數(shù)據(jù)搜索功能,結合實例形式較為詳細的分析了thinkPHP基于getlist實現(xiàn)根據(jù)給定條件進行數(shù)據(jù)的讀取、顯示等相關操作技巧,需要的朋友可以參考下2017-05-05
Symfony2實現(xiàn)在controller中獲取url的方法
這篇文章主要介紹了Symfony2實現(xiàn)在controller中獲取url的方法,實例分析了Symfony獲取URL的常用方法與使用技巧,需要的朋友可以參考下2016-03-03
laravel在中間件內生成參數(shù)并且傳遞到控制器中的2種姿勢
今天小編就為大家分享一篇laravel在中間件內生成參數(shù)并且傳遞到控制器中的2種姿勢,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10

