Php copy public properties

I am looking for a way to copy all public properties from one object to another.

+4
source share
1 answer

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(); } 
+5
source

All Articles