0

Question about implementation relationships between classes. In PHP. It's known that every aggregation is association. But not every association is a aggregation. But what is the difference between them? In terms of implementation.

Of course, there a lot of similar questions and answers.

Here're some answers. But they are almost without some code.

Here're some answers also, with code.

For example, association:

public class Foo { 
    void Baz(Bar bar) {
    } 
}

Aggregation:

public class Foo { 
    private Bar bar; 
    Foo(Bar bar) { 
       this.bar = bar; 
    }
}

However, "If that's the case, both Association and Aggregation code are same. In both cases, 'bar' is just referenced and Bar object may live on". Seems a little bit confusing.

Another example. "Aggregation [...] is the typical whole/part relationship. This is exactly the same as an association with the exception that instances cannot have cyclic aggregation relationships (i.e. a part cannot contain its whole)". Is this the only one difference?

Can you give some example in PHP, showing the difference?

Thanks!

Community
  • 1
  • 1
profport
  • 131
  • 1
  • 1
  • 7
  • The only difference seems to be the fact that in the association case the Bar object is _not necessarily_ stored along with the Foo object, while aggregations require the object to be stored. – bwoebi Sep 02 '16 at 12:09
  • These terms are matter of design which is wider context. It's not a matter how you pass related object instance. Therefore it's not even related with used language. The second link you've provided clearly explains the difference. In aggregations there is owner and child where owner operates on child, in association objects can reference to each other and can work separately. – Jakub Matczak Sep 02 '16 at 12:13
  • "association case the Bar object is not necessarily stored along with the Foo object, while aggregations require the object to be stored" @bwoebi, that makes sense. But in this case what is the difference between dependency and association relationships? E.g. [here](https://nirajrules.wordpress.com/2011/07/15/association-vs-dependency-vs-aggregation-vs-composition/) object is not stored also. In case of dependency. – profport Sep 02 '16 at 13:34
  • @profport every dependency relationship is an association of 1 element. an association may also be an array of values, where a dependency is only one value. – bwoebi Sep 02 '16 at 15:59
  • Are you sure this is php ? – Maaz Rehman Jan 20 '17 at 14:09

0 Answers0