PHP   发布时间:2019-11-11  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了简单谈谈PHP面向对象之标识对象大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

BACkground-color: #ccffcc">

标识对象模式

这个模式主要功能就是创建sql语句中的wehre条件字符串的,下面直接看代码和注释:

php;"> namespace woo\mapper;

//字段对象
class Field {
protected $name = null; //字段名称
protected $operator = null; //操作符
protected $comps = array(); //存放条件的数组
protected $incomplete = false; //检查条件数组是否有值

function __construct ($@R_607_8313@{
$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($field@R_607_8313@{
if(!$this->isVoid()&& $this->currentfield->isIncomplete()){
throw new \Exception("Incomplete field");
}
$this->enforceField($field@R_607_8313@;
if(isset($this->fields[$fieldname]){
$this->currentfield = $this->fields[$fieldname];
} else {
$this->currentfield = new Field($field@R_607_8313@;
$this->fields[$fieldname] = $this->currentfield;
}
return $this; //采用连贯语法
}

//字段集合是否为空
function isVoid(){
return empty($this->fields);
}

//检查字段是否合法
function enforceField ($field@R_607_8313@{
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()+(246060));
$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面向对象之标识对象所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: