title: How do I merge two objects?
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/455700/what-is-the-best-method-to-merge-two-php-objects">What is the best method to merge two PHP objects?</a>  </p>
  </blockquote>
  
  
  
  <p>I have an object, $foo, which has some methods and properties already defined and another object, $bar, which is just a set of properties. I want to merge the entirety of $bar into $foo such that all the properties of $bar become properties of $foo.</p>
  
  <p>So if beforehand I had, $bar->foobar, afterwards I should be able to use $foo->foobar.</p>
  
  <p>At the moment, I am doing the following:</p>
  
  <pre><code>foreach($bar as $key =&gt; $value) {
      $foo-&gt;$key = $value;
      }
  </code></pre>
  
  <p>However, if I were using arrays, I would just do one of the following:</p>
  
  <pre><code>$foo += $bar;
  
  $foo = array_merge($foo, $bar);
  </code></pre>
  
  <p>Is there a similar way of doing this with objects or am I doing it the right way already?</p>
  
  <p>Please note that I do not what $bar to itself become a property of $foo i.e. not $foo->bar->foobar</p>