how are association, aggregation and composition written?

Posted by ajsie on Stack Overflow See other posts from Stack Overflow or by ajsie
Published on 2010-04-24T18:06:08Z Indexed on 2010/04/24 18:13 UTC
Read the original article Hit count: 154

Filed under:
|

i have read some posts about the differences between these 3 relationships and i think i get the point.

i just wonder, are all these written the same when coding?

question 1: all 3 are just a value of the object type in a instance variable?

class A {
    public $b = ''

    public function __construct($object) {
         $this->b = $object // <-- could be a association, aggregation or a composition relation?
    }
}

question 2: does it have to be an instance variable or can it be a static one?

class A {
    public static $b = '' // <-- nothing changed?

    public function __construct($object) {
         $this->b = $object
    }
}

question 3: is there a difference in where the object is created?

i tend to think that composition object is created inside the object:

class A {
    public $b = ''

    public function __construct() {
         $this->b = new Object // is created inside the object
    }
}

and aggregation/association is passed through a constructor or another method:

class A {
    public $b = ''

    public function __construct($object) { // passed through a method
         $this->b = $object
    }
}

question 4: why/when is this important to know. do i have to comment an object inside another what relation its about or do you do it in an UML diagram?

could someone shed a light on these questions.

thanks!

© Stack Overflow or respective owner

Related posts about oop

Related posts about php