PHPackages                             milo/hydrator - 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. milo/hydrator

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

milo/hydrator
=============

Hydrates object from array, exports object to array

v0.3.1(5y ago)2571.9k↓50%3MITPHPPHP &gt;=5.6.0

Since Jul 1Pushed 5y ago5 watchersCompare

[ Source](https://github.com/milo/hydrator)[ Packagist](https://packagist.org/packages/milo/hydrator)[ RSS](/packages/milo-hydrator/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (1)Versions (6)Used By (0)

[![Build Status](https://camo.githubusercontent.com/edd5f20da63ad26074db11e5c7cb0b4cb5762d9d75da32428c6e795d45eebb3e/68747470733a2f2f7472617669732d63692e6f72672f6d696c6f2f6879647261746f722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/milo/hydrator)

Usage
=====

[](#usage)

```
class Person
{
	/** @var string */
	public $name;

	public string $surname;

	public string|null $email;

	/** @var Address[] */
	public array $addresses;
}

class Address
{
	/** @var string */
	public $city;
}

$data = [
	'name' => 'Miloslav',
	'surname' => 'Hůla',
	'addresses' => [
		['city' => 'Prague'],
		['city' => 'Roztoky'],
	],
];

$backend = new Milo\Hydrator\Backend\PublicPropertiesBackend;
$hydrator = new Milo\Hydrator\Hydrator($backend);

/** @var Person $person  created from array */
$person = $hydrator->hydrate(Person::class, $data);
```

Personally, I'm using it for hydrate and export configuration written in [NEON](https://ne-on.org/). Configuration stored in an array is fine, but when grows too much, it's hard to refactor. Working with type hinted objects in IDE is much more comfortable and less error prone.

Backend
=======

[](#backend)

There is only one backend for now.

### [`PublicPropertiesBackend`](src/Hydrator/Backend/PublicPropertiesBackend.php)

[](#publicpropertiesbackend)

It manipulates with public properties of class. It makes type check according a property type definition or `@var` annotation. Annotation type string has to be in a union format (e.g. `int|string|null`). Type checking is strict which means, integer cannot be cast to string or vice versa even it would be possible.

Implement [`Milo\Hydrator\IHydratorBackend`](src/Hydrator/IHydratorBackend.php) for your own backend.

Caching
=======

[](#caching)

Implement [`ICache`](src/Hydrator/Backend/ICache.php) and use it with the backend. It saves resources associated with classes reflection and FQCN (Fully Qualified Class Name) resolving.

I'm using a simple cache class with [Nette Caching](https://github.com/nette/caching). It looks like:

```
use Milo\Hydrator;
use Nette\Caching\Cache;
use Nette\Caching\Storage;

class HydratorCache implements Hydrator\Backend\ICache
{
	private Cache $cache;

	public function __construct(Storage $storage)
	{
		$this->cache = new Cache($storage, 'milo.hydrator');
	}

	public function save($key, $value, array $dependentFiles = null)
	{
		$dependencies = $dependentFiles
			? [Cache::FILES => $dependentFiles]
			: null;

		$this->cache->save($key, $value, $dependencies);
	}

	public function load($key)
	{
		return $this->cache->load($key);
	}
}
```

Installation
============

[](#installation)

Use [Composer](https://getcomposer.org/): `composer require milo/hydrator`

TODO
====

[](#todo)

- improve error messages for deep structures because now are terrible
- support more than one array dimension in Hydrator

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity38

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 70.6% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~538 days

Total

4

Last Release

1993d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/a4c732343c0eba814891adfe91dea1754265ea7a5cbed1232d872cac406ff21e?d=identicon)[milonemilo](/maintainers/milonemilo)

---

Top Contributors

[![milo](https://avatars.githubusercontent.com/u/439140?v=4)](https://github.com/milo "milo (12 commits)")[![grogy](https://avatars.githubusercontent.com/u/1322983?v=4)](https://github.com/grogy "grogy (3 commits)")[![dg](https://avatars.githubusercontent.com/u/194960?v=4)](https://github.com/dg "dg (2 commits)")

### Embed Badge

![Health badge](/badges/milo-hydrator/health.svg)

```
[![Health](https://phpackages.com/badges/milo-hydrator/health.svg)](https://phpackages.com/packages/milo-hydrator)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
