Last active
August 27, 2019 23:21
-
-
Save ChubV/7fdef280ad016ae61b29 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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, thank you, edited
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
Interesting example.
Small fix: duplication variable $data in hydrate.php