PHP Object-oriented "identity mapping"

Source: Internet
Author: User
/* 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.                On the basis of the data mapper, we also added some methods to call the identity map class, the sample code is 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) {s        Elf:: $instance = new 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 Globalkey (DomainObject $obj) {$ke y = Get_class ($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\applicationregistry::g) of the operating database            Etdsn ();            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 objects//(compare the original data mapper code to understand) Private function Getfromap ($    ID) {return \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\doma    In\objectwatcher::add ($obj); }////Compare the code of the original data mapper and find that it is not directly created object but first found in the identity mapping class, cannot find the method called//subclass created and inserted into the identity mapping class, the following find method also follows this principle function createobj              ECT ($array) {              $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 piece of data from the database by ID and create it as an object $old = $this->getf                Rommap ($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\domain    Object $objet);    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, generate a unique identity in the identity mapping class protected    function Targetclass () {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.