博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PHP面向对象之标识对象
阅读量:4357 次
发布时间:2019-06-07

本文共 3294 字,大约阅读时间需要 10 分钟。

/*标识对象模式这个模式主要功能就是创建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')}*/

 

转载于:https://www.cnblogs.com/kerryw/p/7083198.html

你可能感兴趣的文章
div:给div加滚动栏 div的滚动栏设置
查看>>
java随机函数使用方法Random
查看>>
链表中环的入口结点
查看>>
凤姐讲学英语
查看>>
ActionBar
查看>>
5种方法实现数组去重
查看>>
2~15重点语法
查看>>
flask中的CBV,flash,Flask-Session,WTForms - MoudelForm,DBUtils 数据库连接池
查看>>
最近整理的提供免费代理列表的几个网站
查看>>
探偵ガリレオー転写る2
查看>>
快速排序算法C++实现[评注版]
查看>>
七尖记
查看>>
SAP(最短增广路算法) 最大流模板
查看>>
用极大化思想解决矩形问题学习笔记
查看>>
Django REST Framework 简单入门
查看>>
Hibernate中fetch和lazy介绍
查看>>
修改ip脚本
查看>>
解析xlsx与xls--使用2012poi.jar
查看>>
SSL-ZYC 活动安排
查看>>
c# 动态绘制直线和曲线
查看>>