//获取限定的合法字段
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("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'=>''123456')
}
*/
以上这篇简单谈谈PHP面向对象之标识对象就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。