VBAssassin Posted December 11, 2008 Share Posted December 11, 2008 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 Quote Link to comment Share on other sites More sharing options...
corbin Posted December 11, 2008 Share Posted December 11, 2008 As of PHP5, the only time a copy of an object is made is when clone is used. (There are other times too, but for most purposes it's safe to say that.) In PHP5 objects are passed by reference. Quote Link to comment Share on other sites More sharing options...
Eric_Ryk Posted December 12, 2008 Share Posted December 12, 2008 The first is aggregation. The second is really just a collection. A true composite allows for an end node to be used equivalently as a collection. Typically, the collection uses the end nodes via aggregation. Quote Link to comment Share on other sites More sharing options...
448191 Posted December 29, 2008 Share Posted December 29, 2008 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.