简单谈谈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 相关文章推荐
php中的一个中文字符串截取函数
Feb 14 PHP
php使用正则表达式提取字符串中尖括号、小括号、中括号、大括号中的字符串
Apr 05 PHP
VB中的RasEnumConnections函数返回632错误解决方法
Jul 29 PHP
ThinkPHP写数组插入与获取最新插入数据ID实例
Nov 03 PHP
php实现以只读方式打开文件的方法
Mar 16 PHP
PHP+MYSQL中文乱码问题
Jul 01 PHP
PHP中调用C/C++制作的动态链接库的教程
Mar 10 PHP
PHP类的特性实例分析
Sep 28 PHP
thinkphp分页实现效果
Oct 13 PHP
php使用PDO下exec()函数查询执行后受影响行数的方法
Mar 28 PHP
PHP实现上传多图即时显示与即时删除的方法
May 09 PHP
PHP Beanstalkd消息队列的安装与使用方法实例详解
Feb 21 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
网站当前的在线人数
2006/10/09 PHP
codeigniter教程之多文件上传使用示例
2014/02/11 PHP
php实现smarty模板无限极分类的方法
2015/12/07 PHP
深入理解PHP中的count函数
2016/05/31 PHP
laravel5.0在linux下解决.htaccess无效和去除index.php的问题
2019/10/16 PHP
JavaScript中的私有成员
2006/09/18 Javascript
HTML中事件触发列表与解说
2007/07/09 Javascript
Ext中下拉列表ComboBox组件store数据格式用法介绍
2013/07/15 Javascript
TinyMCE汉化及本地上传图片功能实例详解
2016/05/31 Javascript
jQuery自定义元素右键点击事件(实现案例)
2017/04/28 jQuery
深入理解Angular中的依赖注入
2017/06/26 Javascript
浅谈Node Inspector 代理实现
2017/10/19 Javascript
javascript自定义日期比较函数用法示例
2019/07/22 Javascript
js实现点击生成随机div
2020/01/16 Javascript
[04:03]2014DOTA2西雅图国际邀请赛 LGD战队巡礼
2014/07/07 DOTA
tensorflow实现对图片的读取的示例代码
2018/02/12 Python
django 开发忘记密码通过邮箱找回功能示例
2018/04/17 Python
django初始化数据库的实例
2018/05/27 Python
Python openpyxl读取单元格字体颜色过程解析
2019/09/03 Python
Python 多线程,threading模块,创建子线程的两种方式示例
2019/09/29 Python
python模块和包的应用BASE_PATH使用解析
2019/12/14 Python
django ajax发送post请求的两种方法
2020/01/05 Python
Pandas —— resample()重采样和asfreq()频度转换方式
2020/02/26 Python
TensorFlow的reshape操作 tf.reshape的实现
2020/04/19 Python
django Layui界面点击弹出对话框并请求逻辑生成分页的动态表格实例
2020/05/12 Python
numpy 矩阵形状调整:拉伸、变成一位数组的实例
2020/06/18 Python
Scrapy爬虫文件批量运行的实现
2020/09/30 Python
Sandro Paris美国官网:典雅别致的法国时尚服饰品牌
2017/12/26 全球购物
锐步香港官方网上商店:Reebok香港
2020/11/05 全球购物
单位提档介绍信
2014/01/17 职场文书
乡镇党员干部四风对照检查材料思想汇报
2014/09/27 职场文书
2014年艾滋病防治工作总结
2014/12/10 职场文书
逃课检讨书怎么写
2015/01/01 职场文书
2016寒假社会实践心得体会范文
2015/10/09 职场文书
详解CSS中的特指度和层叠问题
2021/07/15 HTML / CSS
使用Redis做预定库存缓存功能
2022/04/02 Redis