PHP物件導向之標識對象

來源:互聯網
上載者:User
本文主要為大家帶來一篇簡單談談PHP物件導向之標識對象。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧,希望能協助到大家。

標識對象模式

這個模式主要功能就是建立sql語句中的wehre條件字串的,下面直接看代碼和注釋:

namespace woo\mapper;//欄位對象class Field {  protected $name = null;     //欄位名稱  protected $operator = null;     //操作符    protected $comps = array();     //存放條件的數組    protected $incomplete = false;   //檢查條件數組是否有值    function __construct ($name){    $this->name= $name;  }    //添加where 條件  function addTest($operator,$value){    $this->comps[] = array('name'=>$this->name,'operator'=>$operator,'value'=>$value);  }    //擷取存放條件的數組  function getComps(){    return $this->comps;  }    function isIncomplete(){    return empty($this->comps);  }}//標識對象class IdentityObject {  protected $currentfield = null;    //當前操作的欄位對象  protected $fields = array();    //欄位集合  private $and = null;  private $enforce = array();      //限定的合法欄位        function __construct($field = null, array $enforce = null){    if(!is_null($enforce)){      $this->enforce = $enforce;    }    if(!is_null($field)){      $this->field($field);    }  }    //擷取限定的合法欄位  function getObjectFields(){    return $this->enforce;  }    //主要功能為設定當前需要操作的對象  function field($fieldname){    if(!$this->isVoid()&& $this->currentfield->isIncomplete()){      throw new \Exception("Incomplete field");    }    $this->enforceField($fieldname);    if(isset($this->fields[$fieldname]){      $this->currentfield = $this->fields[$fieldname];    } else {      $this->currentfield = new Field($fieldname);      $this->fields[$fieldname] = $this->currentfield;    }    return $this;          //採用連貫文法  }    //欄位集合是否為空白  function isVoid(){    return empty($this->fields);  }    //檢查欄位是否合法  function enforceField ($fieldname){    if(!in_array($fieldname,$this->enforce) && !empty($this->enforce)){      $forcelist = implode(',',$this->enforce);      throw new \Exception("{$fieldname} not a legal field {$forcelist}");    }  }      //向欄位對象添加where條件  function eq($value){    return $this->operator("=",$value);  }    function lt($value){    return $this->operator("<",$value);  }    function gt($value){    return $this->operator(">",$value);  }    //向欄位對象添加where條件  private function operator($symbol,$value){    if($this->isVoid){      throw new \Exception("no object field defined");    }    $this->currentfield->addTest($symbol,$value);    return $this;                   //採用連貫文法  }    //擷取此類中所有欄位對象集合的where條件數組  function getComps(){    $ret = array();    foreach($this->fields as $key => $field){      $ret = array_merge($ret,$field->getComps());    }    return $ret;  }}//用戶端代碼$idobj = new IdentityObject ();$idobj->field("name")->eq("The Good Show")->field("start")->gt(time())->lt(time()+(24*60*60));$test = $idobj->getComps();var_dump($test);//輸出類似下面的內容/*array{  array('name'=>'name','operator'=>'=','value'=>'The Good Show'),  array('name'=>'start','operator'=>'>','value'=>'123456'),  //123456表示time()函數輸出的時間戳記  array('name'=>'start','operator'=>'<','value'=>'123456')}*/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.