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

導(dǎo)航首頁 ? 技術(shù)教程 ? 簡(jiǎn)單談?wù)凱HP面向?qū)ο笾畼?biāo)識(shí)對(duì)象
全站頭部文字 我要出現(xiàn)在這里
簡(jiǎn)單談?wù)凱HP面向?qū)ο笾畼?biāo)識(shí)對(duì)象 739 2024-01-10   

標(biāo)識(shí)對(duì)象模式

這個(gè)模式主要功能就是創(chuàng)建sql語句中的wehre條件字符串的,下面直接看代碼和注釋:

namespace woomapper;


//字段對(duì)象
class Field {
  protected $name = null;     //字段名稱
  protected $operator = null;     //操作符  
  protected $comps = array();     //存放條件的數(shù)組  
  protected $incomplete = false;   //檢查條件數(shù)組是否有值
  
  function __construct ($name){
    $this->name= $name;
  }
  
  //添加where 條件
  function addTest($operator,$value){
    $this->comps[] = array('name'=>$this->name,'operator'=>$operator,'value'=>$value);
  }
  
  //獲取存放條件的數(shù)組
  function getComps(){
    return $this->comps;
  }
  
  function isIncomplete(){
    return empty($this->comps);
  }
}


//標(biāo)識(shí)對(duì)象
class IdentityObject {
  protected $currentfield = null;    //當(dāng)前操作的字段對(duì)象
  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;
  }
  
  //主要功能為設(shè)置當(dāng)前需要操作的對(duì)象
  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}");
    }
  }
  
  
  //向字段對(duì)象添加where條件
  function eq($value){
    return $this->operator("=",$value);
  }
  
  function lt($value){
    return $this->operator("<",$value);
  }
  
  function gt($value){
    return $this->operator(">",$value);
  }
  
  //向字段對(duì)象添加where條件
  private function operator($symbol,$value){
    if($this->isVoid){
      throw new Exception("no object field defined");
    }
    $this->currentfield->addTest($symbol,$value);
    return $this;                   //采用連貫語法
  }
  
  //獲取此類中所有字段對(duì)象集合的where條件數(shù)組
  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);

//輸出類似下面的內(nèi)容

/*
array{
  array('name'=>'name','operator'=>'=','value'=>'The Good Show'),
  array('name'=>'start','operator'=>'>','value'=>'123456'),  //123456表示time()函數(shù)輸出的時(shí)間戳
  array('name'=>'start','operator'=>'<','value'=>'123456')
}

*/

以上這篇簡(jiǎn)單談?wù)凱HP面向?qū)ο笾畼?biāo)識(shí)對(duì)象就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持綠夏網(wǎng)。


PHP

主站蜘蛛池模板: 美国伦理三颗熟樱桃| 黄视频免费观看网站| 美少女战士奥特曼| 徐若| 绿门背后| 善良的姐妹| 1—42集分集剧情简介| 思想理论问题| 最后的武士| 少儿不宜视频| 那些年,那些事 电视剧| 黄姓的研究报告怎么写| 国产在线 观看| elles club| 追凶| 牵牛花的生长变化记录| 鲍鱼视频在线观看| 因性而别| 袁雨萱| 电影英雄| 单位同意报考证明| 痴汉电车排名前十番号| 烽火流金电视剧免费观看完整版| 爱你电视剧演员表| 壁纸超清全屏| 战上海老电影战争片子| 春ppt| 《一点》歌曲歌词| 大场面| 每日一句话| 小矮人的一级毛片| 《遇见你之后》电影在线观看| 笼中女电影| 美女拉屎| 电视节目预告表| 荒山之夜| 王李丹妮三级电影| 韩国电影《致命的诱惑》| 山上的小屋 残雪原文| 女性吃鹅肝的好处与功效| 陈诗雅韩国演员|

?。。≌鹃L長期在線接?。。?/p>

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

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

站長微信:lxwl520520

站長QQ:1737366103