Skip to content

Instantly share code, notes, and snippets.

@ChubV
Last active August 27, 2019 23:21
Show Gist options
  • Save ChubV/7fdef280ad016ae61b29 to your computer and use it in GitHub Desktop.
Save ChubV/7fdef280ad016ae61b29 to your computer and use it in GitHub Desktop.
<?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);
}
@devkpv
Copy link

devkpv commented Jan 18, 2016

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

@ChubV
Copy link
Author

ChubV commented Jan 23, 2016

@Spiller, thank you, edited

@jkufner
Copy link

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