keven/hydrate

Hydrate an object from an array of properties.

1.0.7 2019-06-04 09:43 UTC

This package is auto-updated.

Last update: 2024-03-04 20:14:38 UTC


README

Latest Stable Version Build Status License Total Downloads

Hydrate an object from an array of properties.

Install

$ composer install keven/hydrate

Usage

Hydrate a given object:

<?php

$obj = new stdClass;
\Keven\hydrate(array('foo' => 'bar'), $obj);
echo $obj->foo; // "bar"

Hydrate from $this (you don't have to pass the object parameter):

<?php

$car = new Car(array(
    'engine' => 'V8',
    'color'  => 'red',
    'brand'  => 'Chevrolet',
));
echo $car->getColor(); // "red"

class Car
{
    private $engine, $color, $brand;

    public function __construct($args)
    {
        hydrate($args);
    }

    public function getEngine()
    {
        return $this->engine;
    }

    public function getColor()
    {
        return $this->color;
    }

    public function getBrand()
    {
        return $this->brand;
    }
}