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.
blog comments powered by Disqus |
|
|
|
|
|
|
|