成人精品一区二区三区中文字幕-成人精品一区二区三区-成人精品一级毛片-成人精品亚洲-日本在线视频一区二区-日本在线视频免费

導(dǎo)航首頁(yè) ? 技術(shù)教程 ? 老生常談PHP面向?qū)ο笾畼?biāo)識(shí)映射
全站頭部文字 我要出現(xiàn)在這里
老生常談PHP面向?qū)ο笾畼?biāo)識(shí)映射 783 2024-01-10   

標(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 woodomain;

//標(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 woomapper;

abstract class Mapper{      //抽象基類
  abstract static $PDO;    //操作數(shù)據(jù)庫(kù)的pdo對(duì)象
  function __construct (){
    if(!isset(self::$PDO){
      $dsn = woobaseApplicationRegistry::getDSN();
      if(is_null($dsn)){
        throw new woobaseAppException("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 woodomainObjectWatcher::exists($this->targetClass(),$id);
  }
  
  //新增,這里的作用的是將創(chuàng)建的對(duì)象保存起來(lái)
  private function addToMap(woodomainDomainObject $obj){//////
    return woodomainObjectWatcher::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(woodomainDomainObject $obj){      //將對(duì)象數(shù)據(jù)插入數(shù)據(jù)庫(kù)
    $this->doInsert($obj);
    $this->addToMap($obj);            //新增
  }
  
  //需要在子類中實(shí)現(xiàn)的各抽象方法
  abstract function targetClass();//////
  abstract function update(woodomainDomainObject $objet);
  protected abstract function doCreateObject(array $array);
  protected abstract function selectStmt();
  protected abstract function doInsert(woodomainDomainObject $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è)參考,也希望大家多多支持綠夏網(wǎng)。


PHP

主站蜘蛛池模板: 漂流者| 女孩们电影| 十一个月宝宝发育标准| 尹馨演过的三部电影| 新步步惊心| 深流电视剧| 淡蓝色的雨简谱| 电影《森林》| 古天乐电影全部作品最新| 视频素材 网站| 舞法天女第三季| 朱莉与朱莉娅| 水怪电影| 卫途轮胎| 在线播放美脚パンスト女教师| 飞龙猛将演员表| 秦腔《铡美案》全本| 97热| 黄婉秋个人简历| 蕾切尔·布罗斯纳罕| 李顺大造屋| 清水美里| 朋友的女友| 我要逃亡1988国语版免费观看| 协议过户什么意思| 纵横欲海| 影音先锋欧美| 二年级上古诗26首打印| 韩国xxxxxxxxxxxx| 姐妹在线观看| 唐人街探案网剧1| leslie| 国土防线| 土耳其电影水与火| 女人高潮私密按摩视频| kanako| cgtn news在线直播| 苹果恋爱多| 肖传国| 托比·斯蒂芬斯| 王春晖|

!!!站長(zhǎng)長(zhǎng)期在線接!!!

網(wǎng)站、小程序:定制開(kāi)發(fā)/二次開(kāi)發(fā)/仿制開(kāi)發(fā)等

各種疑難雜癥解決/定制接口/定制采集等

站長(zhǎng)微信:lxwl520520

站長(zhǎng)QQ:1737366103