Created
June 27, 2013 14:58
-
-
Save lisachenko/5877138 to your computer and use it in GitHub Desktop.
Fastest hydrator
This file contains hidden or 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 Test { | |
private $a='data'; | |
protected $b=123; | |
public $c=true; | |
} | |
class Hydrator | |
{ | |
private function getContext() | |
{ | |
return get_object_vars($this); | |
} | |
public function __invoke($object) | |
{ | |
static $hydrator = null; | |
if (!$hydrator) { | |
$hydrator = (new ReflectionMethod(__CLASS__, 'getContext'))->getClosure($this); | |
} | |
return $hydrator->bindTo($object, get_class($object))->__invoke($object); | |
} | |
} | |
$a = new Test; | |
$hydrator = new Hydrator(); | |
$data = $hydrator($a); | |
var_dump($data); | |
// OUTPUT: | |
array (size=3) | |
'a' => string 'data' (length=4) | |
'b' => int 123 | |
'c' => boolean true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Not the fastest hydrator =D (also misses writes, which is my current problem :( )
Try running https://github.com/Ocramius/ProxyManager/blob/master/examples/hydrator.php and look at the generated code :)
The problems are with parent classes and private properties, and writes to private properties in general (currently done via reflection).