DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Vector Databases Are Reinventing How Unstructured Data Is Analyzed
  • JQueue: A Library to Implement the Outbox Pattern
  • Troubleshooting Memory Leaks With Heap Profilers
  • Python Memo 2: Dictionary vs. Set

Trending

  • The Full-Stack Developer's Blind Spot: Why Data Cleansing Shouldn't Be an Afterthought
  • Integrating Model Context Protocol (MCP) With Microsoft Copilot Studio AI Agents
  • Metrics at a Glance for Production Clusters
  • Data Quality: A Novel Perspective for 2025
  1. DZone
  2. Data Engineering
  3. Data
  4. Practical PHP Refactoring: Replace Record with Data Class

Practical PHP Refactoring: Replace Record with Data Class

By 
Giorgio Sironi user avatar
Giorgio Sironi
·
Sep. 19, 11 · Interview
Likes (0)
Comment
Save
Tweet
Share
10.8K Views

Join the DZone community and get the full member experience.

Join For Free
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;
    }
}
Database Data (computing) PHP Data structure

Opinions expressed by DZone contributors are their own.

Related

  • Vector Databases Are Reinventing How Unstructured Data Is Analyzed
  • JQueue: A Library to Implement the Outbox Pattern
  • Troubleshooting Memory Leaks With Heap Profilers
  • Python Memo 2: Dictionary vs. Set

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!