keven/hydrate

从属性数组中为对象补水。

1.0.7 2019-06-04 09:43 UTC

This package is auto-updated.

Last update: 2024-09-04 21:12:28 UTC


README

Latest Stable Version Build Status License Total Downloads

从属性数组中为对象补水。

安装

$ composer install keven/hydrate

用法

补水特定对象

<?php

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

$this补水(无需传递对象参数)

<?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;
    }
}