PHP Classes


Recommend this page to a friend!
  Classes of Johnny Mast   Redbox Hydrate   Download  
Role: Documentation
Content type: text/markdown
Description: Documentation
Class: Redbox Hydrate
Popular object variables from array data
Author: By
Last change: Update of
Date: 1 year ago
Size: 4,646 bytes


Class file image Download


Build Status codecov Scrutinizer Code Quality PHP 7.1 Ready HHVM Ready Twitter URL


Hydrating an object is fancy talk for populating properties on PHP objects. This Hydrator helps you to quickly hydrate new class instances and returns the populated result. *Please note* it does not mether if you have private or protected properties in your class it will take care of that for you. Look in the examples folder for basic examples.


I have gone to great length to provide a special (read sexy) and tiny API for you. You can use any kind of API style you see fit in your operation for example.

Static class method

In this example we will hydrate a new instance of class User and return it as $result1 using with with() function.

use Redbox\Hydrate\Hydrator;

 * Method 1
$result1 = Hydrator::hydrate(new User())->with(
        'username' => 'abc',
        'password' => 'pass'

Hydrate function

use function Redbox\Hydrate\Helpers\Hydrate;

 * Method 2
$result2 = Hydrate(new User())->with(
        'username' => 'abc',
        'password' => 'pass'

Instantiate a new Hydrator

Plain old PHP, create your instance of the hydrator then call the with( ) method.

*Please note* There is something off with this method. If your doing this inside a loop it might consume some memory because of all the instances you create. Please don't forget to use the unset function if you are inside any kind of loop.

use Redbox\Hydrate\Hydrator;

 * Method 3
$hydrator = new Hydrator(new User());
$result3 = $hydrator->with(
        'username' => 'abcs',
        'password' => 'pass'

System Requirements

Below are the minimum requirements to use this package:

  • PHP >= 5.40 (But higher is always better)
  • Composer for autoloading


Using composer:

$ composer require redbox/hydrate

Installation trough archive download

If you download the package from a website (for example or or any other) you will need composer installed on your machine. The reason for this is that Redbox-hydrate comes without the vendor directory which is required to run the package.

First of all if you don't have composer installed you can find it here. Follow the instructions and please don't get intimidated in fact its really really easy to install.

In the this sample i will assume you have composer installed (on any machine). Go to the package root (where composer.json is located) and execute the following command.

$ composer install  --no-dev

Now your almost ready to go. In your project require the redbox-hydrate.php (located in the package root). Assuming that Redbox-hydrate was installed in ./lib/redbox-hydrate/ your php file would look like this.

require 'lib/redbox-hydrate/redbox-hydrate.php';
// more of your nice code below

And you are ready use Redbox-hydrate in your application.


Redbox-hydrate is created and maintained by Johnny Mast. For feature requests or suggestions you could consider sending me an e-mail.


Redbox-hydrate is released under the MIT public license.



Oh and if you've come down this far, you might as well follow me on twitter.