I'm looking for a way to copy all the public properties from one object to 开发者_Python百科another.
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();
}
精彩评论