3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Hoge { private $a = 'foo'; private $b = 'bar'; } function updateProperties($instance, array $properties) { $ref = new ReflectionClass($instance); $props = $ref->getProperties(ReflectionProperty::IS_PRIVATE); foreach ($props as $prop) { if (isset($properties[$prop->name])) { $prop->setAccessible(true); $prop->setValue($properties[$prop->name]); } } } $h = new Hoge; updateProperties($h, array('a' => 'fizz', 'b' => 'buzz')); var_dump($h);

preferences:
32.63 ms | 402 KiB | 5 Q