3v4l.org

run code in 300+ PHP versions simultaneously
<?php function profile($c, $n = 1000) { $m = 'microtime'; for ($i = $n * is_callable($c), $t = 0;$i--;$w = $m(1), $c(), $t+= ($m(1) - $w) / $n); return $t; } class Foo { protected $foo; protected $bar; protected $baz; } class FooSelfHydratingProxy extends Foo { /** zero-argument constructor since this is just a helper class for hydration */ public function __construct() { } /** * the `Foo` typehint is not part of the interface - here only for the sake of readability/clearness */ public function hydrate($data, Foo $object) { $object->foo = $data['foo']; $object->bar = $data['bar']; $object->baz = $data['baz']; } /** * the `Foo` typehint is not part of the interface - here only for the sake of readability/clearness */ public function extract(Foo $object) { return array('foo' => $object->foo, 'bar' => $object->bar, 'baz' => $object->baz); } } // data $data = array('foo' => 1, 'bar' => 2, 'baz' => 3); // lastguest function dehydrate($obj){ $class = get_class($obj); $exp = var_export($obj,1); return eval('return '.substr($exp,strpos($exp,'(')+1,-1).';'); } function static_hydrate($class,$dry=null){ static $hydrators = array(); $h = '_Hydrator__'.$class; if (empty($hydrators[$class])) { eval("class $h extends $class { static function __hydrate(\$a) { \$o = new $class(); foreach (\$a as \$n=>\$v) \$o->\$n=\$v; return \$o; } } function hydrate_$class(\$o){return $h::__hydrate(\$o);}" ); $hydrators[$class] = true; } return $dry?$h::__hydrate($dry):new $h; } function hydrate($class,$dry=null){ static $hydrators = array(); $h = '_Hydrator__'.$class; if (empty($hydrators[$class])) { eval("class $h extends $class { static function __hydrate(\$a) { \$o = new $class(); foreach (\$a as \$n=>\$v) \$o->\$n=\$v; return \$o; } } function hydrate_$class(\$o){return $h::__hydrate(\$o);}" ); $hydrators[$class] = true; } return $dry?$h::__hydrate($dry):new $h; } $hydrator = new FooSelfHydratingProxy(); echo 'Ocramius: ',profile(function() use ($data,$hydrator) { $hydrated = new Foo(); $hydrator->hydrate($data, $hydrated); })*1E6,PHP_EOL; $hydr = static_hydrate('Foo'); echo 'Lastguest: ',profile(function() use ($data) { hydrate_Foo($data); })*1E6,PHP_EOL; $hydr = static_hydrate('Foo'); echo 'Lastguest: ',profile(function() use ($data,$hydr) { $hydr->__hydrate($data); })*1E6,PHP_EOL; $foo = hydrate_Foo($data); $foo_s = serialize($foo); echo 'Lastguest: ',profile(function() use ($data,$foo_s) { unserialize($foo_s); })*1E6,PHP_EOL;

preferences:
42.37 ms | 402 KiB | 5 Q