最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PHP面向?qū)ο笾ぷ鲉卧?實(shí)例講解)

 更新時(shí)間:2017年06月26日 08:08:12   投稿:jingxian  
下面小編就為大家?guī)硪黄狿HP面向?qū)ο笾ぷ鲉卧?實(shí)例講解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

工作單元

這個(gè)模式涉及到了領(lǐng)域模型、數(shù)據(jù)映射器和標(biāo)識映射,這里就統(tǒng)一進(jìn)行整理和回顧了。

$venue = new \woo\domain\Venue(null,"The Green Tree");

\woo\domain\ObjectWatcher::instance()->performOperations();

現(xiàn)在以上面的二行客戶端代碼為切入點(diǎn)大概的敘述一下這個(gè)模式是怎么工作的。

第一句在使用領(lǐng)域模型對象創(chuàng)建一個(gè)對象的時(shí)候,它就調(diào)用了標(biāo)識映射ObjectWatcher類

將自己標(biāo)記為一個(gè)需要新增的對象。第二句的performOperations方法將保存在標(biāo)識映射器的屬性$new中的對象

插入到了數(shù)據(jù)庫中。注意它內(nèi)部調(diào)用的$obj->finder()方法是領(lǐng)域模式中通過HelperFactory工廠類生成一個(gè)相對應(yīng)的數(shù)據(jù)映射器類并return過來。

HelperFactory這個(gè)類下面沒有具體實(shí)現(xiàn)(原文也沒有實(shí)現(xiàn)),其實(shí)就是根據(jù)參數(shù)傳入的類的類型使用條件分支創(chuàng)建對應(yīng)的數(shù)據(jù)映射器。

下面直接看代碼和注釋進(jìn)行理解。

namespace woo\domain;

//標(biāo)識映射
class ObjectWatcher{
  
  private $all = array();        //存放對象的小倉庫
  private $dirty = array();      //存放需要在數(shù)據(jù)庫中修改的對象
  private $new = array();        //存放需要在數(shù)據(jù)庫中新增的對象
  private $delete = array();      //存放需要在數(shù)據(jù)庫中刪除的對象
  private static $instance;      //單例
  
  
  private function __construct (){}
  
  static function instance(){
    if(!self::$instance){
      self::$instance = new ObjectWatcher();
    }
    return self::$instance;
  }
  
  //獲取一個(gè)唯一的標(biāo)識,這里采用了領(lǐng)域類類名+ID的方式創(chuàng)建一個(gè)唯一標(biāo)識,避免多個(gè)數(shù)據(jù)庫表調(diào)用這個(gè)類時(shí)出現(xiàn)ID重復(fù)的問題
  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;
  }
  
  //標(biāo)記為需要?jiǎng)h除的對象
  static function addDelete(DomainObject $obj){
    $self = self::instance();
    $self->delete[$self->globalKey($obj)] = $obj;
  }
  
  //標(biāo)記為需要修改的對象
  static function addDirty(DomainObject $obj){
    $inst = self::instance();
    if(!in_array($obj,$inst->new,true)){
      $inst->dirty[$inst->globalKey($obj)] = $obj;
    }
  }
  
  //標(biāo)記為需要新增的對象
  static function addNew(DomainObject $obj){
    $inst = self::instance();
    $inst->new[] = $obj;
  }
  
  //標(biāo)記為干凈的對象
  static function addClean(DomainObject $obj){
    $self = self::instance();
    unset($self->delete[$self->globalKey($obj)]);
    unset($self->dirty[$self->globalKey($obj)]);
    $self->new = array_filter($self->new,function($a) use($obj) {return !($a === $obj);});
  }
    
  //將上述需要增刪改的對象與數(shù)據(jù)庫交互進(jìn)行處理  
  function performOperations(){
    foreach($this->dirty as $key=>$obj){
      $obj->finder()->update($obj);    //$obj->finder()獲取一個(gè)數(shù)據(jù)映射器
    }
    foreach($this->new as $key=>$obj){
      $obj->finder()->insert($obj);
    }
    $this->dirty = array();
    $this->new = array();
  }
}


//領(lǐng)域模型
abstract class DomainObject{      //抽象基類
  
  private $id = -1;
  
  function __construct ($id=null){
    if(is_null($id)){
      $this->markNew();      //初始化時(shí)即被標(biāo)記為需要新增的對象了
    } else {
      $this->id = $id;
    }  
  }
  
  //調(diào)用了標(biāo)識映射的標(biāo)記對象的方法
  function markNew(){
    ObjectWatcher::addNew($this);
  }
  
  function markDeleted(){
    ObjectWatcher::addDelete($this);
  }
  
  function markDirty(){
    ObjectWatcher::addDirty($this);
  }
  
  function markClean(){
    ObjectWatcher::addClean($this);
  }
  
  function setId($id){
    $this->id = $id;
  }
  
  function getId(){
    return $this->id;
  }
  
  
  function finder(){
    return self::getFinder(get_class($this));
  }
  
  //通過工廠類來實(shí)例化一個(gè)特定類型的數(shù)據(jù)映射器對象,例如VenueMapper
  //這個(gè)對象將被標(biāo)識映射器中的performOperations方法調(diào)用用于與數(shù)據(jù)庫交互進(jìn)行增刪改的操作
  static function getFinder($type){
    return HelperFactory::getFinder($type);
  }
  
}


class Venue extends DomainObject {
  private $name;
  private $spaces;
  
  function __construct ($id = null,$name=null){
    $this->name= $name;
    $this->spaces = self::getCollection('\\woo\\domain\\space'); 
    parent::__construct($id);
  }
  
  function setSpaces(SpaceCollection $spaces){
    $this->spaces = $spaces;
    $this->markDirty();            //標(biāo)記為需要修改的對象
  }
  
  function addSpace(Space $space){
    $this->spaces->add($space);
    $space->setVenue($this);
    $this->markDirty();            //標(biāo)記為需要修改的對象
  }
  
  function setName($name_s){
    $this->name = $name_s;
    $this->markDirty();            //標(biāo)記為需要修改的對象
  }
  
  function getName(){
    return $this->name;
  }
}


//領(lǐng)域模型
class Space extends DomainObject{
  //.........
  function setName($name_s){
    $this->name = $name_s;
    $this->markDirty();
  }
  
  function setVenue(Venue $venue){
    $this->venue = $venue;
    $this->markDirty();
  }
}


//數(shù)據(jù)映射器
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);
    }
  }
  
  //獲取標(biāo)記的對象
  private function getFroMap($id){
    return \woo\domain\ObjectWatcher::exists($this->targetClass(),$id);
  }
  
  
  //新增標(biāo)記的對象
  private function addToMap(\woo\domain\DomainObject $obj){//////
    return \woo\domain\ObjectWatcher::add($obj);
  }
  
  
  //將數(shù)據(jù)庫數(shù)據(jù)映射為對象
  function createObject($array){
    $old = $this->getFromMap($array['id']);
    if($old){return $old;}
    $obj = $this->doCreateObject($array);
    $this->addToMap($obj);
    $obj->markClean();
    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);            
  }
  
  //需要在子類中實(shí)現(xiàn)的各個(gè)抽象方法
  abstract function targetClass();                    //獲取類的類型
  abstract function update(\woo\domain\DomainObject $objet);        //修改操作
  protected abstract function doCreateObject(array $array);        //創(chuàng)建對象
  protected abstract function selectStmt();                //查詢操作
  protected abstract function doInsert(\woo\domain\DomainObject $object);  //插入操作
  
}

class VenueMapper extends Mapper {
  function __construct (){    
    parent::__construct();  
    //預(yù)處理對象
    $this->selectStmt = self::$PDO->prepare("select * from venue where id=?");
    $this->updateStmt = self::$PDO->prepare("update venue set name=?,id=? where id=?");
    $this->insertStmt = self::$PDO->prepare("insert into venue (name) values(?)");
  }
  
  protected function getCollection(array $raw){    //將Space數(shù)組轉(zhuǎn)換成對象集合
    return new SpaceCollection($raw,$this);        
  }
  
  protected function doCreateObject (array $array){  //創(chuàng)建對象
    $obj = new \woo\domain\Venue($array['id']);
    $obj->setname($array['name']);
    return $obj;
  }
  
  protected function doInsert(\woo\domain\DomainObject $object){ //將對象插入數(shù)據(jù)庫
    print 'inserting';
    debug_print_backtrace();
    $values = array($object->getName());
    $this->insertStmt->execute($values);
    $id = self::$PDO->lastInsertId();
    $object->setId($id);
  }
  
  function update(\woo\domain\DomainObject $object){    //修改數(shù)據(jù)庫數(shù)據(jù)
    print "updation\n";
    $values = array($object->getName(),$object->getId(),$object->getId());
    $this->updateStmt->execute($values);
  }
  
  function selectStmt(){          //返回一個(gè)預(yù)處理對象
    return $this->selectStmt;
  }
  
}


//客戶端
$venue = new \woo\domain\Venue(null,"The Green Tree");        //在初始化時(shí)就被標(biāo)記為新增對象了
$venue->addSpace(new \woo\domain\Space(null,"The Space Upstairs"));  //這二行addSpace方法因?yàn)関enue已經(jīng)被標(biāo)記新增所以不會(huì)再標(biāo)記為修改對象,但是space在初始化的時(shí)候會(huì)被標(biāo)記為新增對象
$venue->addSpace(new \woo\domain\Space(null,"The Bar Stage"));      
\woo\domain\ObjectWatcher::instance()->performOperations();      //與數(shù)據(jù)庫交互新增一條Venue數(shù)據(jù),以及二條space數(shù)據(jù)

以上這篇PHP面向?qū)ο笾ぷ鲉卧?實(shí)例講解)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 檢測codeigniter腳本消耗內(nèi)存情況的方法

    檢測codeigniter腳本消耗內(nèi)存情況的方法

    這篇文章主要介紹了檢測codeigniter腳本消耗內(nèi)存情況的方法,實(shí)例分析了codeigniter中memory_usage方法及{ memory_usage}偽變量的使用技巧,需要的朋友可以參考下
    2015-03-03
  • PHP實(shí)現(xiàn)加密文本文件并限制特定頁面的存取的效果

    PHP實(shí)現(xiàn)加密文本文件并限制特定頁面的存取的效果

    本篇文章主要介紹了PHP實(shí)現(xiàn)加密文本文件并限制特定頁面的存取,可以限制用戶對某些頁面的存取,有需要的可以了解一下。
    2016-10-10
  • CodeIgniter多語言實(shí)現(xiàn)方法詳解

    CodeIgniter多語言實(shí)現(xiàn)方法詳解

    這篇文章主要介紹了CodeIgniter多語言實(shí)現(xiàn)方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了CodeIgniter實(shí)現(xiàn)多語言的具體步驟、實(shí)現(xiàn)方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2016-01-01
  • ThinkPHP模板輸出display用法分析

    ThinkPHP模板輸出display用法分析

    這篇文章主要介紹了ThinkPHP模板輸出display用法,以實(shí)例形式詳細(xì)分析了ThinkPHP使用display調(diào)用各類模板輸出的用法,是非常常見的實(shí)用技巧,需要的朋友可以參考下
    2014-11-11
  • php函數(shù)版本更新的方法和使用工具

    php函數(shù)版本更新的方法和使用工具

    更新php函數(shù)版本至關(guān)重要,可提高安全性、性能和代碼可維護(hù)性,詳細(xì)描述:評估影響:確定依賴于過時(shí)函數(shù)的代碼并評估更新影響,制定計(jì)劃:制定分階段更新計(jì)劃,從不重要函數(shù)開始,編寫測試用例:驗(yàn)證更新后函數(shù)的行為,逐步更新:分批更新函數(shù),逐一徹底測試
    2024-10-10
  • PHP單例模式是什么 php實(shí)現(xiàn)單例模式的方法

    PHP單例模式是什么 php實(shí)現(xiàn)單例模式的方法

    PHP單例模式是什么?這篇文章主要介紹了php實(shí)現(xiàn)單例模式的方法,告訴大家為什么使用單例模式,感興趣的朋友可以參考一下
    2016-05-05
  • Zend Framework動(dòng)作助手Redirector用法實(shí)例詳解

    Zend Framework動(dòng)作助手Redirector用法實(shí)例詳解

    這篇文章主要介紹了Zend Framework動(dòng)作助手Redirector用法,結(jié)合實(shí)例形式詳細(xì)分析了轉(zhuǎn)向器Redirector的功能,使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2016-03-03
  • 一個(gè)實(shí)用的php驗(yàn)證碼類

    一個(gè)實(shí)用的php驗(yàn)證碼類

    這篇文章主要為大家詳細(xì)介紹了一個(gè)實(shí)用的php驗(yàn)證碼類,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • Yii框架視圖、視圖布局、視圖數(shù)據(jù)塊操作示例

    Yii框架視圖、視圖布局、視圖數(shù)據(jù)塊操作示例

    這篇文章主要介紹了Yii框架視圖、視圖布局、視圖數(shù)據(jù)塊操作,結(jié)合實(shí)例形式分析了Yii框架相關(guān)的視圖、布局、控制器及數(shù)據(jù)相關(guān)操作技巧,需要的朋友可以參考下
    2019-10-10
  • PHP面試題之文件目錄操作

    PHP面試題之文件目錄操作

    本篇文章是我在之前面試這家公司時(shí)遇到的問題,當(dāng)時(shí)代碼寫的不全,后來通過查閱相關(guān)資料,整理出來的一份分享給大家
    2015-10-10

最新評論

永靖县| 金堂县| 泽州县| 南华县| 阳新县| 连南| 阜宁县| 壤塘县| 全椒县| 阿瓦提县| 长春市| 惠水县| 尉犁县| 德江县| 莫力| 盖州市| 政和县| 湖北省| 岫岩| 霍州市| 凤翔县| 罗江县| 沾化县| 成都市| 台东市| 奈曼旗| 遵义县| 龙江县| 皋兰县| 茌平县| 焉耆| 资源县| 永靖县| 南岸区| 武强县| 铜川市| 平泉县| 栖霞市| 蕲春县| 西乡县| 东兴市|