Agile Zone is brought to you in partnership with:

I am a programmer and architect (the kind that writes code) with a focus on testing and open source; I maintain the PHPUnit_Selenium project. I believe programming is one of the hardest and most beautiful jobs in the world. Giorgio is a DZone MVB and is not an employee of DZone and has posted 635 posts at DZone. You can read more from them at their website. View Full User Profile

Practical PHP Refactoring: Replace Record with Data Class

09.19.2011
| 5273 views |
  • submit to reddit
We often find ourselves tempted by the shortcut of using directly a record-like data structure provided by the language or a framework. There are many example scenarios where a record emerges:
  • when you use a database for persistence (not only a with relational one) there can be a data structure containing the results of a query.
  • when you use associative arrays, often they have a small number of fixed keys.

Record-like structures are the equivalent of C structs, or Ruby hashes. This refactoring is a generalization of Replace Array with Object: in this case the starting point is not just an array which always has the same number and type of fields, but any data structure homoegeneous to it:

  • Zend_Db_Table_Row, implementing a Row Data Gateway and giving access to a row in the database.
  • stdClass instances (or arrays) fetched by PDOStatement.

In some languages (see C) the record is a particular data structure which is not an object; in PHP, it is always an array or an object of some vendor class.

Even ORMs based on Active Record are more advanced than this kind of usage: they usually let you add methods on the model classes, which are populated with data by the ORM itself. In this refactoring, we are talking about a data structure managed by the language of the persistence layer, and whose code you cannot modify.

Why replacing a record-like structure?

Generic classes do not let you add methods to manipulate their data: when using directly a Zend_Db_Table_Row or an associative array for storage of the result of a query, you have to resort continuously to foreign methods. Each time you need new logic, you have to compromise encapsulation on the object itself, and these methods get duplicated in various instance of client code.

Solutions

There are different ways to eliminate the coupling to a record-like structure.

The first is to refactor to subclassing, where the data structure becomes an Active Record. You extends the vendor class with your own one: this option is only available if the structure is defined as an object.

Moreover, the name of the class to instantiate must be configurable in the persistence mechanism. Zend_Db_Table_Record supports this kind of usage.

A second option is to refactor to composition: Zend_Db examples exist also for this case. This approach can be used to avoid large hierarchies: your model classes compose the Zend_Db objects and hide the database; methods can be added for once at your own models.

A third and final alternative is to use hydration: data is copied to your model objects, and records are thrown away after the fact. Doctrine 2 and Data Mappers in general choose this approach.

Steps

I will describe a short procedure for refactoring to hydration since it is the most complex approach and it is always applicable. The other are instead specific for the particular data structure (for example with Zend_Db you have to write some subclasses and configure some protected fields to contain the right class names.)

  1. Create a new class, as one of your models. Its state should be represented by one row (or more rows joined into one) in the database.
  2. This class should gain a private field for each of the record's fields, usually with getters and setters.
  3. This class should accept in the constructor or in a Factory Method an instance of the record, so that it can produce a new instance.

If you want to decouple from the persistsnce, or you want to go two ways (also save and not only visualize, since this is not just a presentation model), look for a Data Mapper such as Doctrine 2, which will even hide all the record structures from you and manager associations where objects compose other ones.

Example

In the example, the data of a single user are returned in an array. I chose an array as the record-like structure to minimize the external dependencies of this code.

<?php
/**
 * The test case works as an example of client code, as always.
 */
class ReplaceRecordWithDataClass extends PHPUnit_Framework_TestCase
{
    public function testShowContainedData()
    {
        $users = new UsersTable();
        $giorgio = $users->find(42);
        $this->assertEquals('Giorgio', $giorgio['name']);
    }
}

/**
 * This is a Fake Table Data Gateway. The machinery for making it work with
 * a database will be distracting for our purposes, so they will be omitted.
 */
class UsersTable
{
    /**
     * @return mixed    the returned value can be a Zend_Db_Table_Row,
     *                  an Active Record, a stdClass, an associative array...
     *                  It should just represent a single entity.
     */
    public function find($id)
    {
        // execute a PDOStatement and fetch the data
        return array('id' => 42, 'name' => 'Giorgio');
    }
}

After the refactoring, we have a User class where we can add all the methods we need:

<?php
/**
 * The test case works as an example of client code, as always.
 */
class ReplaceRecordWithDataClass extends PHPUnit_Framework_TestCase
{
    public function testShowContainedData()
    {
        $users = new UsersTable();
        $giorgio = $users->find(42);
        $this->assertEquals('Giorgio', $giorgio->getName());
    }
}

/**
 * This is a Fake Table Data Gateway. The machinery for making it work with
 * a database will be distracting for our purposes, so they will be omitted.
 */
class UsersTable
{
    /**
     * @return mixed    the returned value can be a Zend_Db_Table_Row,
     *                  an Active Record, a stdClass, an associative array...
     *                  It should just represent a single entity.
     */
    public function find($id)
    {
        // execute a PDOStatement and fetch the data
        return User::fromRecord(array('id' => 42, 'name' => 'Giorgio'));
    }
}

class User
{
    private $id;
    private $name;

    public static function fromRecord(array $record)
    {
        $object = new self();
        $object->id = $record['id'];
        $object->name = $record['name'];
        return $object;
    }

    public function getName()
    {
        return $this->name;
    }
}
Published at DZone with permission of Giorgio Sironi, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

eli lindner replied on Wed, 2012/01/11 - 9:37am

Hi

 

For interest of completion your code accesses private properties to hydrate the object. 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.