PHP Object-oriented mapping of identity maps and texts

Source: Internet
Author: User
Tags dsn map class
This article mainly introduces PHP object-oriented mapping of the graphics and text, interested in the reference of friends, I hope to help you.

The identity map adds an identity mapping class based on the data mapper, and the main function is to save the objects that have already been created, and to get the degradation of the system performance directly, rather than repeating the creation, when needed.

A partial invocation of the identity mapping class is also added on the basis of the data mapper, with the sample code as follows:

Namespace woo\domain;//identity Map class objectwatcher{Private $all = Array ();      Store the object's small warehouse private static $instance; Singleton private Function __construct () {} static function instance () {if (!self:: $instance) {self:: $instance = n    EW Objectwatcher ();  } return Self:: $instance; }//Gets a unique identity, where the domain class class name +id is used to create a unique identity that avoids duplicate ID problems when multiple database tables call this class function Globalkey (DomainObject $obj) {$key = Get_cl ($obj). "." .    $obj->getid ();  return $key;    }//Add object static function Add (DomainObject $obj) {$inst = Self::instance ();  $inst->all[$inst->globalkey ($obj)] = $obj;    }//Gets the object static function exists ($classname, $id) {$inst = Self::instance ();    $key = "$classname. $id";    if (Isset ($inst->all[$key]) {return $inst->all[$key];  } return null;    }}namespace Woo\mapper;abstract class mapper{//abstract static $PDO; The PDO object function __construct () {if!isset (self:: $PDO) {$dsn = \woo\base\applicationregistr) of the operating databaseY::getdsn ();      if (Is_null ($DSN)) {throw new \woo\base\appexception ("No DNS");      } self:: $PDO = new \pdo ($DSN);    Self:: $PDO->setattribute (\pdo::attr_errmode,\pdo::errmode_exception); }}//Data mapper based on new methods the following will simply be added, here is to get the object instead of querying the database and repeatedly create the object//(compare the original data mapper code to understand) Private function Getfromap ($id) {RE  Turn \woo\domain\objectwatcher::exists ($this->targetclass (), $id); }//New, the function here is to save the created object to private function Addtomap (\woo\domain\domainobject $obj) {//////return \woo\domain\objectw  Atcher::add ($obj); }////Compare the code of the original data mapper and find that it is not directly created object but first looked up in the identity map class, not found in the method called//subclass created and inserted into the identity mapping class, the following find method also follows this principle function CreateObject ($arra  Y) {$old = $this->getfrommap ($array [' id ']);  New if ($old) {return $old}//New $obj = $this->docreateobject ($array);          Implement $this->addtomap ($obj) in subclasses;  Added return $obj;        }//function find ($id) {//Get a data from the database by ID and create as Object $old = $this->getfrommap ($id); //New if ($old) {return $old}//New $this->selectstmt ()->execute (Array ($id));    $array = $this->selectstmt ()->fetch ();    $this->selectstmt ()->closecursor ();    if (!is_array ($array)) {return null;    } if (!isset ($array [' ID '])) {return null;    } $object = $this->createobject ($array);          $this->addtomap ($object);    Added return $object;    } function Insert (\woo\domain\domainobject $obj) {//Inserts the object data into the database $this->doinsert ($obj);            $this->addtomap ($obj); NEW}//abstract function Targetclass () for each abstraction method that needs to be implemented in subclasses;//////abstract function update (\woo\domain\domainobject $ob  Jet);  protected abstract function Docreateobject (array $array);  protected abstract function selectstmt (); protected abstract function Doinsert (\woo\domain\domainobject $object);} Class Spacemapper extends Mapper {///other code in the Data Mapper article has been implemented here to skip/....//class name, the protected function t that generates a unique identity in the identity mapping class Argetclass () {return "woo\\Domain\\space "; }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.