PHP 객체지향 식별 객체

小云云
풀어 주다: 2023-03-20 09:00:01
원래의
2094명이 탐색했습니다.

이 기사에서는 주로 PHP의 객체 지향 식별 객체에 대해 간략하게 설명합니다. 편집자님이 꽤 좋다고 생각하셔서 지금 공유하고 모두에게 참고용으로 드리고자 합니다. 편집자를 따라가서 모두에게 도움이 되기를 바랍니다.

식별 개체 모드

이 모드의 주요 기능은 SQL 문에서 조건부 문자열을 생성하는 것입니다. 바로 아래 코드와 주석을 참조하세요.

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'=>'<&#39;,&#39;value&#39;=>'123456')
}

*/
로그인 후 복사

관련 권장 사항:

JavaScript 개체 지향에 대한 새로운 이해

JavaScript 객체지향 정의 멤버 메소드 인스턴스 분석

PHP 면접 질문 중 객체지향 질문 분석

위 내용은 PHP 객체지향 식별 객체의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!