這篇文章主要介紹“如何實(shí)現(xiàn)PHP面向?qū)ο笾畼?biāo)識(shí)映射”,在日常操作中,相信很多人在如何實(shí)現(xiàn)PHP面向?qū)ο笾畼?biāo)識(shí)映射問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對(duì)大家解答”如何實(shí)現(xiàn)PHP面向?qū)ο笾畼?biāo)識(shí)映射”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!

標(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ì)象的小倉庫
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ù)庫表調(diào)用這個(gè)類時(shí)出現(xiàn)ID重復(fù)的問題
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ù)庫的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ì)簡稱新增,這里的作用的是獲取對(duì)象而不是查詢數(shù)據(jù)庫并重復(fù)創(chuàng)建對(duì)象
//(對(duì)比一下原數(shù)據(jù)映射器的相關(guān)代碼即可了解)
private function getFroMap($id){
return \woo\domain\ObjectWatcher::exists($this->targetClass(),$id);
}
//新增,這里的作用的是將創(chuàng)建的對(duì)象保存起來
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){ //通過ID從數(shù)據(jù)庫中獲取一條數(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ù)庫
$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)這里略過
//.............
//類名,在標(biāo)識(shí)映射類中生成標(biāo)識(shí)用的
protected function targetClass(){
return "woo\\domain\\Space";
}
}到此,關(guān)于“如何實(shí)現(xiàn)PHP面向?qū)ο笾畼?biāo)識(shí)映射”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
網(wǎng)頁標(biāo)題:如何實(shí)現(xiàn)PHP面向?qū)ο笾畼?biāo)識(shí)映射-創(chuàng)新互聯(lián)
標(biāo)題路徑:http://chinadenli.net/article26/hdgjg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、用戶體驗(yàn)、微信公眾號(hào)、虛擬主機(jī)、手機(jī)網(wǎng)站建設(shè)、自適應(yīng)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容