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

Building a generic domain object class - 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Before I start showing you how to build a data mapper class whose responsibility will be focused on decoupling domain objects from the underlying persistence mechanism, it's necessary to define the class that will permit you to create the concrete domain objects. This class will be an abstract one, and its definition will look like this:

(DomainObjectAbstract.php)

 

 

<?php

 

 

abstract class DomainObjectAbstract

{

    protected $_data = array();

   

    public function __construct(array $data = NULL)

    {

        if ($data !== NULL)

        {

            // populate domain object with an array of data

            foreach ($data as $property => $value)

            {

                if (!empty($property))

                {

                   $this->$property = $value;

                }

            }

        }

    }

   

    // set domain object property

    public function __set($property, $value)

    {

        if (!array_key_exists($property, $this->_data))

        {

            throw new ModelObjectException('The specified property is not valid for this domain object.'); 

        }

        if (strtolower($property) === 'id' AND $this->_data['id'] !== NULL)

        {

            throw new DomainObjectException('ID for this domain object is immutable.');

        }

        $this->_data[$property] = $value;

    }

   

    // get domain object property

    public function __get($property)

    {

        if (!array_key_exists($property, $this->_data))

        {

            throw new DomainObjectException('The property requested is not valid for this domain object.');

        }

        return $this->_data[$property];

    } 

   

    // check if given domain object property has been set

    public function __isset($property)

    {

        return isset($this->_data[$property]);

    }

   

    // unset domain object property

    public function __unset($property)

    {

        if (isset($this->_data[$property]))

        {

            unset($this->_data[$property]);

        }

    }

}

 

 

 

(DomainObjectException.php)

 

 

<?php

 

 

class DomainObjectException extends Exception{}

Essentially, the previous "DomainObjectAbstract" class is only a simple data container that adds and deletes domain object properties on the fly, thanks to the implementation of the "__set()," "__get()" and "__unset()" PHP 5 magic methods. Additionally, the class throws a couple of custom exceptions at the proper places, in case the client code attempts to assign an invalid property to the domain object being manipulated.

Apart from understanding the inner workings of this class, which is fairly straightforward, you should notice that it models common objects, or resembling the phrase coined in the Java world, plain old PHP objects (POPO). This obviously makes testing and maintaining domain objects a breeze, as they aren't tied to any specific storage mechanism.

Finally, I'd like to mention that the approach that I followed here to create this abstract domain object class was partially inspired by the example included in Pádraic Brady's online book. 

Having clarified that point, and now that the generic structure of the domain objects has been set, the next logical step is to derive a subclass from the parent "DomainObjectAbstract" to create some concrete domain objects.

To learn more about how this subclass will be defined, click on the link that appears below and keep reading.



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- 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: