开发者

Php copy public properties

开发者 https://www.devze.com 2023-01-18 16:44 出处:网络
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?

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();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消