What is a Hydrator in Zend Framework 2

What is a Hydrator ?


Almost anyone who was new to Zend Framework 2 asked me this Question.
Some Zend Framework 2 examples make excessive usage of this new hydrator concept, some real life examples make even more excessive usage or even use it too much. When you search for a Hydrator on the web you find mostly one explanation Hydrator or Hydrate “to hydrate: to combine chemically with water”. You even could find articles about a Hydrator in Back to the Future or various cosmetic products.

Some people ask me if Hydrator is a Design Pattern ? So far it not a design Pattern listed in the GOF Book or any other Standard Design patterns list i know,  so obviously there is no Hydrator Design Pattern, the only occurrence in Software Development i found were Doctrine and Zend Framework 2.


So let´s have a look what this Zend Framework 2 Data Hydrator is about.

With ZF 2 the Hydrator concept was introduced to the Zend Framework World.

Every Hydrator needs to implement 2 Functions,

extract($object) returns - the array presentation of an object
hydrate(array $data, $object) - will hydrate the object with the data given in $data array.

The hydrators are mostly build for usage with Form and DataTable objects, mostly at any point where you need to transfer an object from an array, or any other type of data.

In conclusion:

Transforming an Object into Array Data (extract) or the other way arround (hydrate)


Predefined Hydrators

There are some predefined Hydrators which cover the most common purposes.
A Hydrator is described in the HydratorInterface Hydrator Interface Zend\Stdlib\Hydrator

namespace Zend\Stdlib\Hydrator;

interface HydratorInterface
     * Extract values from an object
     * @param  object $object
     * @return array
    public function extract($object);

     * Hydrate $object with the provided $data.
     * @param  array $data
     * @param  object $object
     * @return object
    public function hydrate(array $data, $object);

ClassMethods Hydrator

Class Methods The ClassMethod Hydrator is using get* and set* functions of the hydrated object to “hydrate”

ObjectProperty Hydrator

Object Property The ObjectProperty Hydrator is very similar to the classMethods Hydrator but is using the public object properties instead of the getter and setter values.

Reflection Hydrator

Reflection< Is Like ObjectProperty Hydrator but makes private and protected properties accessible to be hydrated or extracted. Mostly this concept does not seem like something you commonly use caus there is a reason if your properties are private, more like for special usage only.

ArraySerializable Hydrator

ArraySerializable Is for objects implementing the ArraySerializable interface.


Is the Abstract parent class of all other Hydrators we know so far. So far it is not that complicated you got 4 Simple Hydrators which cover the most common usages we have dealing with Forms or other Standard procedures. But finally that is not everything a hydrator can do, there is also an enhanced functionality, besides just “mapping” Objects to Arrays wich would simply just be a Mapper


Filters and Strategies

Filters Besides that functionality the Hydrator offers additional functions by adding a Strategy or Filters, that makes the usage a lot more complex. With Filters you can exclude (blacklist) or whitelist specific methods or properties from the extraction. They are described in detail in http://framework.zend.com/manual/2.1/en/modules/zend.stdlib.hydrator.filter.html

created: 2013-05-04 08:01:42
by: John Behrens
comments powered by Disqus