Creating an array to object hydrator
The Hydrator pattern is a variation of the Data Transfer Object design pattern. Its design principle is quite simple: moving data from one place to another. In this illustration, we will define classes to move data from an array to an object.
How to do it...
- First, we define a
Hydrator
class that is able to use getters and setters. For this illustration we will useApplication\Generic\Hydrator\GetSet
:namespace Application\Generic\Hydrator; class GetSet { // code }
- Next, we define a
hydrate()
method, which takes both an array and an object as arguments. It then calls thesetXXX()
methods on the object to populate it with values from the array. We useget_class()
to determine the object's class, and thenget_class_methods() ...
Get PHP 7 Programming Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.