是否可以一次为PHP中的对象设置多个属性?
而不是做:
而不是做:
$object->prop1 = $something; $object->prop2 = $otherthing; $object->prop3 = $morethings;
做类似的事情:
$object = (object) array( 'prop1' => $something,'prop2' => $otherthing,'prop3' => $morethings );
但不覆盖对象.
不像你想要的那样.但这可以通过使用循环来完成.
$map = array( 'prop1' => $something,'prop3' => $morethings ); foreach($map as $k => $v) $object->$k = $v;
仅查看2条额外的线条.