Skip to content

Instantly share code, notes, and snippets.

@ChubV
Last active Aug 27, 2019
Embed
What would you like to do?
<?php
class Foo
{
private $foo;
private $bar;
private $baz;
}
$hydrator = function(array $data) {
$this->foo = $data['a'];
$this->bar = $data['b'];
$this->baz = $data['c'];
};
$hydrated = new Foo();
$data = array('a' => 1, 'b' => 2, 'c' => 3);
$aHydrator = $hydrator->bindTo($hydrated, $hydrated);
$aHydrator($data);
var_dump($hydrated);
<?php
$iterations = 100000;
class A {private $a; private $b; private $c;}
$hydrator = function(array $data) {
$this->a = $data['a'];
$this->b = $data['b'];
$this->c = $data['c'];
};
$data = array('a' => 1, 'b' => 2, 'c' => 3);
$a = new A();
for ($i = 0; $i < $iterations; $i++) {
$aHydrator = $hydrator->bindTo($a, $a);
$aHydrator($data);
}
@SpiLLeR

This comment has been minimized.

Copy link

@SpiLLeR SpiLLeR commented Jan 18, 2016

Interesting example.
Small fix: duplication variable $data in hydrate.php

@ChubV

This comment has been minimized.

Copy link
Owner Author

@ChubV ChubV commented Jan 23, 2016

@SpiLLeR, thank you, edited

@jkufner

This comment has been minimized.

Copy link

@jkufner jkufner commented Aug 27, 2019

Thanks, this is an interesting idea.

It seems that Closure::call() might be a better way to do this trick.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment