Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize PHP object-oriented identity Mapping

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to achieve PHP object-oriented identity mapping". In daily operation, I believe that many people have doubts about how to achieve PHP object-oriented identity mapping. I have consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to achieve PHP object-oriented logo mapping". Next, please follow the editor to study!

Identity mapping adds an identity mapping class to the data mapper. The main function is to save the created objects, which can be directly obtained rather than repeatedly created when needed, resulting in a decline in system performance.

In addition to the data mapper, a part of the method that calls the identity mapping class is added. The sample code is as follows:

Namespace woo\ domain;// identity mapping class class ObjectWatcher {private $all = array (); / / small warehouse private static $instance; / / singleton private function _ _ construct () {} static function instance () {if (! self::$instance) {self::$instance = new ObjectWatcher ();} return self::$instance } / / get a unique identity. Here, the domain class name + ID is used to create a unique identity to avoid ID duplication when multiple database tables call this class. Function globalKey (DomainObject $obj) {$key = get_class ($obj). "." $obj- > getId (); return $key;} / add object static function add (DomainObject $obj) {$inst = self::instance (); $inst- > all [$inst- > globalKey ($obj)] = $obj;} / / get 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 base class abstract static $PDO; / / pdo object function _ _ construct () {if (! isset (self::$PDO) {$dsn =\ woo\ base\ ApplicationRegistry::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) }} / / the new method based on the data mapper will be abbreviated below, which is used to get the object instead of querying the database and repeatedly creating the object / / (compare the relevant code of the original data mapper to see) private function getFroMap ($id) {return\ woo\ domain\ ObjectWatcher::exists ($this- > targetClass (), $id) } / / added, the purpose here is to save the created object to private function addToMap (\ woo\ domain\ DomainObject $obj) {/ return\ woo\ domain\ ObjectWatcher::add ($obj) } / / comparing the code of the original data mapper, we find that it does not create an object directly, but first looks for it in the identity mapping class, and the method of the / / subclass that is called after it cannot be found is created and inserted into the identity mapping class. The following find method also follows this principle: function createObject ($array) {$old = $this- > getFromMap ($array ['id']) / / add if ($old) {return $old} / / add $obj = $this- > doCreateObject ($array); / / implement $this- > addToMap ($obj) in the subclass; / / add return $obj;} / / function find ($id) {/ / get a piece of data from the database through ID and create it as an object $old = $this- > getFromMap ($id) / / add if ($old) {return $old} / / add $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); / / add return $object;} function insert (\ woo\ domain\ DomainObject $obj) {/ / insert object data into the database $this- > doInsert ($obj); $this- > addToMap ($obj); / / add} / / various abstract methods to be implemented in subclasses abstract function targetClass (); / abstract function update (\ woo\ domain\ DomainObject $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 has been implemented in the data mapper. / / class name, generate the protected function targetClass () {return "woo\\ domain\\ Space" for unique identification in the identity mapping class;}} at this point, the study on "how to implement PHP object-oriented identity mapping" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report