Have you tried the get_object_vars function?
foreach(get_object_vars($a) as $prop => $value) { $b->$prop = $value; }
A more modern approach would be to use Reflection :
$reflect = new ReflectionClass($a); foreach($reflect->getProperties(ReflectionProperty::IS_PUBLIC) as $prop) { $name = $prop->getName(); $b->$name = $prop->getValue(); }
source share