簡(jiǎn)單談?wù)凱HP面向?qū)ο笾畼?biāo)識(shí)對(duì)象
標(biāo)識(shí)對(duì)象模式
這個(gè)模式主要功能就是創(chuàng)建sql語(yǔ)句中的wehre條件字符串的,下面直接看代碼和注釋:
namespace woo\mapper;
//字段對(duì)象
class Field {
protected $name = null; //字段名稱
protected $operator = null; //操作符
protected $comps = array(); //存放條件的數(shù)組
protected $incomplete = false; //檢查條件數(shù)組是否有值
function __construct ($name){
$this->name= $name;
}
//添加where 條件
function addTest($operator,$value){
$this->comps[] = array('name'=>$this->name,'operator'=>$operator,'value'=>$value);
}
//獲取存放條件的數(shù)組
function getComps(){
return $this->comps;
}
function isIncomplete(){
return empty($this->comps);
}
}
//標(biāo)識(shí)對(duì)象
class IdentityObject {
protected $currentfield = null; //當(dāng)前操作的字段對(duì)象
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;
}
//主要功能為設(shè)置當(dāng)前需要操作的對(duì)象
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; //采用連貫語(yǔ)法
}
//字段集合是否為空
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}");
}
}
//向字段對(duì)象添加where條件
function eq($value){
return $this->operator("=",$value);
}
function lt($value){
return $this->operator("<",$value);
}
function gt($value){
return $this->operator(">",$value);
}
//向字段對(duì)象添加where條件
private function operator($symbol,$value){
if($this->isVoid){
throw new \Exception("no object field defined");
}
$this->currentfield->addTest($symbol,$value);
return $this; //采用連貫語(yǔ)法
}
//獲取此類中所有字段對(duì)象集合的where條件數(shù)組
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);
//輸出類似下面的內(nèi)容
/*
array{
array('name'=>'name','operator'=>'=','value'=>'The Good Show'),
array('name'=>'start','operator'=>'>','value'=>'123456'), //123456表示time()函數(shù)輸出的時(shí)間戳
array('name'=>'start','operator'=>'<','value'=>'123456')
}
*/
以上這篇簡(jiǎn)單談?wù)凱HP面向?qū)ο笾畼?biāo)識(shí)對(duì)象就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
php使用正則驗(yàn)證密碼字段的復(fù)雜強(qiáng)度原理詳細(xì)講解
密碼涉及到安全問(wèn)題,所以密碼設(shè)計(jì)的復(fù)雜程序尤為重要。通常密碼由大小寫字母、數(shù)字、特殊符號(hào)組成。一般情況下前端可不做js判斷,使用ajax提交php后端程序接口,由后端判斷后返回json數(shù)據(jù),進(jìn)行用戶密碼復(fù)雜程序提示。2022-11-11
PHP中使用GD庫(kù)創(chuàng)建圓形餅圖的例子
這篇文章主要介紹了PHP中使用GD庫(kù)創(chuàng)建圓形餅圖的例子,本文給出了的代碼例子實(shí)現(xiàn)了一個(gè)扇形統(tǒng)計(jì)圖,需要的朋友可以參考下2014-11-11
php小型企業(yè)庫(kù)存管理系統(tǒng)的設(shè)計(jì)與實(shí)現(xiàn)代碼
雙專業(yè)畢業(yè)論文做了一個(gè)小型企業(yè)庫(kù)存管理系統(tǒng),使用PHP&mysql組合進(jìn)行開發(fā)。頭一次開發(fā)稍微大一點(diǎn)的東西,php也是自學(xué)的2011-05-05
通過(guò)PHP簡(jiǎn)單實(shí)例介紹文件上傳
本文通過(guò)代碼實(shí)例給大家介紹php文件上傳,獲取文件名稱、類型、大小等相關(guān)信息,完成文件的上傳,本文非常具有參考借鑒價(jià)值,特此分享到腳本之家平臺(tái),供大家學(xué)習(xí)2015-12-12
微信小程序頁(yè)面向下滾動(dòng)時(shí)tab欄固定頁(yè)面頂部實(shí)例講解
這篇文章主要介紹了微信小程序頁(yè)面向下滾動(dòng)時(shí)tab欄固定頁(yè)面頂部實(shí)例講解,文中圖文實(shí)例講解的很透徹,有需要的同學(xué)可以參考下2021-03-03
Yii框架擴(kuò)展CGridView增加導(dǎo)出CSV功能的方法
這篇文章主要介紹了Yii框架擴(kuò)展CGridView增加導(dǎo)出CSV功能的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Yii框架擴(kuò)展組件實(shí)現(xiàn)導(dǎo)出CSV格式數(shù)據(jù)的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-05-05

