PHPackages                             redbox/hydrate - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. redbox/hydrate

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

redbox/hydrate
==============

v1.0(9y ago)0211MITPHPPHP &gt;=5.4.0CI failing

Since Sep 9Pushed 9y ago1 watchersCompare

[ Source](https://github.com/johnnymast/redbox-hydrator)[ Packagist](https://packagist.org/packages/redbox/hydrate)[ RSS](/packages/redbox-hydrate/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (1)Dependencies (1)Versions (2)Used By (0)

[![redbox-logo-klein](https://cloud.githubusercontent.com/assets/121194/18277406/ac4d9868-744e-11e6-8994-86943704d0d4.png)](https://cloud.githubusercontent.com/assets/121194/18277406/ac4d9868-744e-11e6-8994-86943704d0d4.png)

[![Build Status](https://camo.githubusercontent.com/d0577371439aa28a1f9536beb70c05416aace065cdd0e36ef24fae3590900568/68747470733a2f2f7472617669732d63692e6f72672f6a6f686e6e796d6173742f726564626f782d6879647261746f722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/johnnymast/redbox-hydrator)[![codecov](https://camo.githubusercontent.com/5243d60cf96d3c2ae63c5fed2105e39c76b160943c95a48e730a46f1ddea2141/68747470733a2f2f636f6465636f762e696f2f67682f6a6f686e6e796d6173742f726564626f782d6879647261746f722f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/johnnymast/redbox-hydrator)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/39136244e7e9eb13ea7bd86928e477edb34831e1334afc89deb7bd0add6341c8/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6a6f686e6e796d6173742f726564626f782d6879647261746f722f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/johnnymast/redbox-hydrator/?branch=master)[![PHP 7.1 Ready](https://camo.githubusercontent.com/40478dd353dd4db5db1a8aa569ff4ce978c0f3bb20339912919e1707fa15ce59/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4848564d2d52656164792d677265656e2e737667)](http://hhvm.com/)[!\[HHVM Ready\](https://img.shields.io/badge/PHP 7.1-Ready-green.svg)](http://hhvm.com/)[!\[Twitter URL\](https://img.shields.io/twitter/url/http/shields.io.svg?style=social&amp;label=Contact author)](https://twitter.com/intent/tweet?text=@mastjohnny)

Redbox-hydrate
==============

[](#redbox-hydrate)

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](https://github.com/johnnymast/redbox-hydrator/tree/master/examples).

Examples
--------

[](#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

[](#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
----------------

[](#hydrate-function)

```
use function Redbox\Hydrate\Helpers\Hydrate;

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

Instantiate a new Hydrator
--------------------------

[](#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'
    ]
);
unset($hydrator);
```

System Requirements
-------------------

[](#system-requirements)

Below are the minimum requirements to use this package:

- PHP &gt;= 5.40 (But higher is always better)
- [Composer](https://getcomposer.org/) for autoloading

Installation
------------

[](#installation)

Using [composer](https://packagist.org/packages/redbox/hydrate):

```
$ composer require redbox/hydrate
```

Installation trough archive download
------------------------------------

[](#installation-trough-archive-download)

If you download the package from a website (for example [github.io](https://github.com/johnnymast/redbox-hydrator/) or [phpclasses.org](http://www.phpclasses.org/package/9929-PHP-Quickly-Hydrate-classes-from-arrays.html) 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](https://getcomposer.org/). 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.

```
