Welcome Guest, Not a member yet? Register   Sign In
Library -Object - Hydrate
#5

Not really answering the question here, but one use case I have for "hydrating" is when you'd want to create a number of objects of the same type, and you want to eliminate the instantiation cost (most likely because the constructor does some heavy operations). For example:

Code:
class ExampleFactory {

    protected $prototypes = [];
    protected $hydrators  = [];

    public function configure($prototype, Callable $hydrator)
    {
        $class = get_class($prototype);
        $this->prototypes[$class] = $prototype; // This is a "bare" object
        $this->hydrators[$class]  = $hydrator;
    }

    public function create($class, $data)
    {
        $object = clone $this->prototypes[$class]; // The real magic is here - we don't instantiate, but copy
        call_user_func($this->hydrators[$class], $object, $data); // The hydrator should know how to populate the bare object
        return $object;
    }
}

(don't use this, it's just an example)
Reply


Messages In This Thread
Library -Object - Hydrate - by martin88 - 11-14-2016, 02:36 PM
RE: Library -Object - Hydrate - by ciadmin - 11-14-2016, 03:03 PM
RE: Library -Object - Hydrate - by martin88 - 11-14-2016, 03:21 PM
RE: Library -Object - Hydrate - by ciadmin - 11-14-2016, 04:05 PM
RE: Library -Object - Hydrate - by Narf - 11-15-2016, 02:56 AM
RE: Library -Object - Hydrate - by martin88 - 11-16-2016, 03:04 PM



Theme © iAndrew 2016 - Forum software by © MyBB