Jump to content

Recommended Posts

Hi ya,

 

Are these code snippet showing aggregation or composition?

 

Example:

<?php
class poster {

}
class wall {

function render_poster(poster $poster) {

}

}
$poster = new poster();
$wall = new wall();
$wall->render_poster($poster);
?>

 

Example 2:

<?php
class poster {

}
class wall {

private $posters = array();

function add_poster(poster $poster) {
	$this->posters[] = $poster;
}

}
$poster = new poster();
$wall = new wall();
$wall->add_poster($poster);
?>

 

Notice that the second example is NOT being passed by reference (or does PHP5 auto pass by reference unless you use a clone statement or put & in the methods parameter list?).

 

I think i know what i would class them as in my head, but just want your opinions to make sure i'm correct, otherwise, i'm going to be asking some more questions if i'm wrong :-D

 

Kind regards,

Scott

Link to comment
https://forums.phpfreaks.com/topic/136523-composition-or-aggregation/
Share on other sites

  • 3 weeks later...

Both are examples of aggregation (multiplicity is not a factor in that).

 

By the strict definition of composite aggregation, "the whole consists of its parts". Add to that the requirement of life cycle dependency (i.e. if the "whole" is destroyed, so will the "parts") and you can imagine composition is not a very common phenomenon. Although not everybody takes this definition as strict. Many people just use the life cycle dependency rule.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.