久久久久在线观看_又色又爽又黄的免费视频播放_一区中文字幕_日韩电影在线播放

今日焦點 焦點資訊 營銷之道 企業報道 淘寶運營 網站建設 軟件開發 400電話
  當前位置: 首頁 » 資訊 » 軟件開發 » 正文

PHP面向對象之標識對象實例詳解

放大字體  縮小字體 發布日期:2018-02-10  來源:企業800網  作者:新格網  瀏覽次數:185  【去百度看看】
本文主要為大家帶來一篇簡單談談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);

//輸出類似下面的內容

相關推薦:

關于Javascript面向對象的新認識

js面向對象之繼承知識詳解

Javascript面向對象實現猜拳游戲

以上就是PHP面向對象之標識對象實例詳解的詳細內容,更多請關注php中文網其它相關文章!

 
關鍵詞: php,實例,對象
 
[ 資訊搜索 ]  [ 加入收藏 ]  [ 告訴好友 ]  [ 打印本文 ]  [ 違規舉報 ]  [ 關閉窗口 ]

 
0條 [查看全部]  相關評論

 
網站首頁 | 關于我們 | 聯系方式 | 使用協議 | 版權隱私 | 網站地圖 | 排名推廣 | 廣告服務 | 積分換禮 | 網站留言 | RSS訂閱 | 皖ICP備2021004516號-14
企業800網 · 提供技術支持