PHP Classes

Building an Identity Map in PHP: Retrieve objects avoiding multiple instances

Recommend this page to a friend!
     
  Info   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStar 52%Total: 503 All time: 5,803 This week: 129Up
Version License PHP version Categories
php-identity-map 16BSD License5.0PHP 5, Databases, Design Patterns
Description 

Author

This package can store and retrieve objects in persistent storage containers avoiding to have multiple instances of the same object in memory.

It can use a mapper class to store objects of a class in a container like for instance a database table.

It can also retrieve the objects from the container assuring that only one instance of the same object is retrieved into memory.

Picture of Gjero Krsteski
  Performance   Level  
Innovation award
Innovation award
Nominee: 2x

 

Details

Building an Identity Map in PHP

Sample application used for training.

This example code is no production code and should be used for training purposes only.

This example code requires:

  • PDO a lightweight, consistent interface for accessing databases in PHP.
  • PHPUnit a unit testing framework for PHP projects.

This example code implements:

  • Data-Mapper Pattern
  • Identity-Map Pattern

Why identity mapping?

By using Data-Mapper pattern without an identity map, you can easily run into problems because you may have more than one object that references the same domain entity.

Data-Mapper without identity map

  $userMapper = new UserMapper($pdo);

  $user1 = $userMapper->find(1); // creates new object
  $user2 = $userMapper->find(1); // creates new object

  echo $user1->getNickname(); // joe123
  echo $user2->getNickname(); // joe123

  $user1->setNickname('bob78');

  echo $user1->getNickname(); // bob78
  echo $user2->getNickname(); // joe123 -> ?!?

Data-Mapper with identity map

The identity map solves this problem by acting as a registry for all loaded domain instances.

  $userMapper = new UserMapper($pdo);

  $user1 = $userMapper->find(1); // creates new object
  $user2 = $userMapper->find(1); // returns same object

  echo $user1->getNickname(); // joe123
  echo $user2->getNickname(); // joe123

  $user1->setNickname('bob78');

  echo $user1->getNickname(); // bob78
  echo $user2->getNickname(); // bob78 -> yes, much better

By using an identity map you can be confident that your domain entity is shared throughout your application for the duration of the request.

Note that using an identity map is not the same as adding a cache layer to your mappers. Although caching is useful and encouraged, it can still produce duplicate objects for the same domain entity.

Load the Data-Mappers with the Repository class

  $repository = new Repository($this->db);
  $userMapper = $repository->load('User');
  $insertId = $userMapper->insert(new User('billy', 'gatter'));
  

  Files folder image Files (22)  
File Role Description
Files folder imagedatabase (2 files)
Files folder imagesrc (1 file, 3 directories)
Files folder imagetests (1 file, 2 directories)
Plain text file autoload.php Class autoloder
Accessible without login Plain text file phpunit.xml.dist Data phpunit configuration
Accessible without login Plain text file README.markdown Doc. README
Accessible without login Plain text file test-bootstrap.php Aux. bootstraping

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:503
This week:0
All time:5,803
This week:129Up
User Ratings User Comments (1)
 All time
Utility:66%StarStarStarStar
Consistency:58%StarStarStar
Documentation:66%StarStarStarStar
Examples:-
Tests:66%StarStarStarStar
Videos:-
Overall:52%StarStarStar
Rank:2448