Home arrow PHP arrow Page 2 - Implementing the Data Mapper Design Pattern in PHP 5

The Data Mapper pattern: an introductory example - PHP

In this first part of a five-part series, I introduce you to implementing the Data Mapper design pattern in PHP 5. This pattern attempts to solve the issue of the strong coupling that often exists between the domain objects present in an application and the underlying persistence mechanism. This benefit comes at a cost, since data mappers add a new layer of complexity. Living with this minor trade-off is quite worthwhile, though, as you'll see.

  1. Implementing the Data Mapper Design Pattern in PHP 5
  2. The Data Mapper pattern: an introductory example
  3. Building a generic domain object class
  4. Creating concrete domain objects
By: Alejandro Gervasio
Rating: starstarstarstarstar / 7
March 10, 2010

print this article



As mentioned in the introduction, the implementation of the Data Mapper design pattern allows you to isolate all of the domain objects present in an application from the persistence mechanism in a pretty elegant and efficient way. Naturally, this brief description is somewhat obscure and abstract, so to shed some light, here's a contrived example that shows how to use a data mapper to perform CRUD operations on some persistence-ignorant user domain objects. The example in question is as follows:




// get instance of MySQL adapter class

$db = MySQL::getInstance();



// create instance of 'UserMapper' class

$userMapper = new UserMapper($db);



// get existing user by ID

$user = $userMapper->find(1);

// display user data

echo 'ID: '. $user->id . '<br />First Name: ' . $user->fname . '<br />Last Name: ' . $user->lname . '<br />Email: ' . $user->email;



// create a new user and add her to the database

$user = new User();

$user->fname = 'Sandra';

$user->lname = 'Smith';

$user->email = 'sandra@domain.com';




// update existing user

$user = $userMapper->find(2);

$user->fname = 'Sandy';




// delete existing user


The above code sample shows how to use an instance of data mapper class to perform CRUD operations on a couple of user objects. As you can see, the data mapper is aware of the storage mechanism, which in this case happens to be a simple MySQL database, but the domain objects are completely ignorant of it. In consequence, it's the responsibility of the data mapper to execute the pertinent database operations, a process that not only permits decoupling the domain objects from the persistence layer, but makes those objects much easier to test and maintain.

Of course, this is only an introductory example that hides all of the complexities of the Data Mapper pattern behind a clean interface. My plan for this series, though, is to build a complete example from scratch that covers the development of the domain object classes, along with the associated data mappers, so get ready to read a lot of code samples. You've been warned!

At this point, you should have a more appropriate idea of the advantages to using the Data Mapper design pattern specifically in PHP 5, so it's time to start defining the sample classes required to implement it in a useful way. With that idea in mind, in the following segment I'm going to build the first of these classes, which will define the generic structure and behavior of the domain objects.

To learn how this initial class will be defined, click on the link below and read the next few lines.

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: