title: Encapsulation Aggregation / Composition
body: <p>The Wikipedia  article about encapsulation states:</p>
  
  <p>"Encapsulation also protects the integrity of the component, by preventing users from setting the internal data of the component into an invalid or inconsistent state"</p>
  
  <p>I started a discussion about encapsulation on a forum, in which I asked whether you should always clone objects inside setters and/or getters as to preserve the above rule of encapsulation. I figured that, if you want to make sure the objects inside a main object aren't tampered with outside the main object, you should always clone it.</p>
  
  <p>One discussant argued that you should make a distinction between aggregation and composition in this matter. Basically what I think he ment is this:</p>
  
  <ul>
  <li>If you want to return an object that is part of a composition (for instance, a Point of a Rectangle), clone it.</li>
  <li>If you want to return an object that is part of aggregation (for instance, a User as part of a UserManager), just return it without breaking the reference.</li>
  </ul>
  
  <p>That made sense to me too. But now I'm a bit confused. And would like to have your opinions on the matter.</p>
  
  <p>Strictly speaking, does encapulation always mandate cloning?</p>
  
  <p>PS.: I program in PHP, where resource management might be a little more relevant, since it's a scripted language.</p>