简单谈谈PHP面向对象之标识对象


Posted in PHP onJune 27, 2017

标识对象模式

这个模式主要功能就是创建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')
}

*/

以上这篇简单谈谈PHP面向对象之标识对象就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

PHP 相关文章推荐
discuz程序的PHP加密函数原理分析
Aug 05 PHP
PHP中使用数组实现堆栈数据结构的代码
Feb 05 PHP
php中flush()、ob_flush()、ob_end_flush()的区别介绍
Feb 17 PHP
使用php统计字符串中中英文字符的个数
Jun 23 PHP
php ckeditor上传图片文件名乱码解决方法
Nov 15 PHP
Yii把CGridView文本框换成下拉框的方法
Dec 03 PHP
php清除和销毁session的方法分析
Mar 19 PHP
PHP实现统计在线人数功能示例
Oct 15 PHP
php遍历、读取文件夹中图片并分页显示图片的方法
Nov 15 PHP
php_pdo 预处理语句详解
Nov 21 PHP
Zend Framework动作控制器用法示例
Dec 09 PHP
laravel5.6 框架操作数据 Eloquent ORM用法示例
Jan 26 PHP
什么是PHP文件?如何打开PHP文件?
Jun 27 #PHP
PHP面向对象之工作单元(实例讲解)
Jun 26 #PHP
thinkphp框架page类与bootstrap分页(美化)
Jun 25 #PHP
解决出现SoapFault (looks like we got no XML document)的问题
Jun 24 #PHP
php-fpm开启状态统计的方法详解
Jun 23 #PHP
PHP多种序列化/反序列化的方法详解
Jun 23 #PHP
PHP后端银联支付及退款实例代码
Jun 23 #PHP
You might like
骨王战斗力在公会成员中排不进前五,却当选了会长,原因竟是这样
2020/03/02 日漫
德生S2000收音机更换“钕铁硼”全频扬声器
2021/03/02 无线电
php自定义函数之递归删除文件及目录
2010/08/08 PHP
php下使用curl模拟用户登陆的代码
2010/09/10 PHP
解析php中mysql_connect与mysql_pconncet的区别详解
2013/05/15 PHP
浅谈php7的重大新特性
2015/10/23 PHP
PHP实现补齐关闭的HTML标签
2016/03/22 PHP
js 动态添加标签(新增一行,其实很简单,就是几个函数的应用)
2009/03/26 Javascript
js 获取范围内的随机数实例代码
2016/08/02 Javascript
AngularJs directive详解及示例代码
2016/09/01 Javascript
js 判断附件后缀的简单实现方法
2016/10/11 Javascript
Vue2.0组件间数据传递示例
2017/03/07 Javascript
Extjs表单输入框异步校验的插件实现方法
2017/03/20 Javascript
angular中使用Socket.io实例代码
2017/06/03 Javascript
JS实现身份证输入框的输入效果
2017/08/21 Javascript
Angular实现可删除并计算总金额的购物车功能示例
2017/12/26 Javascript
vue+SSM实现验证码功能
2018/12/07 Javascript
JavaScript中的类型检查
2020/02/03 Javascript
在Linux中通过Python脚本访问mdb数据库的方法
2015/05/06 Python
Python爬虫通过替换http request header来欺骗浏览器实现登录功能
2018/01/07 Python
Python对excel文档的操作方法详解
2018/12/10 Python
图文详解python安装Scrapy框架步骤
2019/05/20 Python
python numpy实现文件存取的示例代码
2019/05/26 Python
django 通过URL访问上传的文件方法
2019/07/28 Python
使用Python脚本zabbix自定义key监控oracle连接状态
2019/08/28 Python
在Django中预防CSRF攻击的操作
2020/03/13 Python
利用python控制Autocad:pyautocad方式
2020/06/01 Python
在Keras中CNN联合LSTM进行分类实例
2020/06/29 Python
HTML5的结构和语义(1):前言
2008/10/17 HTML / CSS
html5 拖拽及用 js 实现拖拽功能的示例代码
2020/10/23 HTML / CSS
澳大利亚工具仓库:Tools Warehouse
2018/10/15 全球购物
Arti-shopping中文官网:大型海外商品一站式直邮平台
2020/03/23 全球购物
指针和引用有什么区别
2013/01/13 面试题
写给爸爸的道歉信
2014/01/15 职场文书
殡葬服务心得体会
2014/09/11 职场文书
励志广播稿300字(5篇)
2014/09/15 职场文书