The following small series for everyone to bring a brief talk about PHP object-oriented identity object. Small series feel very good, now share to everyone, also for everyone to make a reference. Let's take a look at it with a little knitting.
Identity object Pattern
The main function of this mode is to create the Wehre condition string in the SQL statement, and look directly at the code and comments below:
namespace woo\mapper;//Field object class Field {protected $name = null; Field name protected $operator = NULL; Operator protected $comps = Array (); Array protected $incomplete = False for the storage condition; Checks if the condition array has value function __construct ($name) {$this->name= $name; }//Add Where Condition function addtest ($operator, $value) {$this->comps[] = Array (' name ' = = $this->name, ' operator ' =& gt; $operator, ' value ' = $value); }//Gets the array function getcomps () {return $this->comps; that holds the condition } function Isincomplete () {return empty ($this->comps); }}//Identity Object class Identityobject {protected $currentfield = null; The Field object of the current operation protected $fields = Array (); Field collection Private $and = null; Private $enforce = Array (); Qualified legal field function __construct ($field = null, array $enforce = null) {if (!is_null ($enforce)) {$this->enf Orce = $enforce; } if (!is_null ($field)) {$this->field ($field); }}//Get Qualified legal field function Getobjectfields () {return $this->enforce; }//The main function is to set the Object function field ($fieldname) {if (! $this->isvoid () && $this->currentfield->isinco Mplete ()) {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; Use coherent syntax}//field collection is null function isvoid () {return empty ($this->fields); }//Check if the field is legal 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}"); }}//Add a Where condition to the Field object function eq ($value) {return $this->operator ("=", $value); } function lt ($value) {return $this->operator ("<", $Value); } function gt ($value) {return $this->operator (">", $value); }//Add a Where condition to the Field object private function operator ($symbol, $value) {if ($this->isvoid) {throw new \exception ("No O bject field defined "); } $this->currentfield->addtest ($symbol, $value); return $this; Use coherent syntax}//Get the Where condition array for all the Field object collections in this class function Getcomps () {$ret = array (); foreach ($this->fields as $key = + $field) {$ret = Array_merge ($ret, $field->getcomps ()); } return $ret; }}//client Code $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);//output similar to the following/*array{array (' name ' = > ' name ', ' operator ' = ' = ', ' value ' = ' The Good Show '), Array (' name ' = = ' start ', ' operator ' = ' > ', ' Value ' = = ' 123456 '),//123456 represents the time () function output timestamp array (' name ' = ' = ' start ', ' operator ' = ' < ', ' value ' = = ') 123456 ')}*/