Skip to content

Instantly share code, notes, and snippets.

@lisachenko
Created June 27, 2013 14:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lisachenko/5877138 to your computer and use it in GitHub Desktop.
Save lisachenko/5877138 to your computer and use it in GitHub Desktop.
Fastest hydrator
<?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
@Ocramius
Copy link

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).

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