Jump to content

Recommended Posts

Hi. I was wondering which one of the samples below would be right to set as object association.

 

===snippet 01===

$p1 = new Person(10, 'John Doe', '31/07/1967');
$a1 = new Account;
$a1->accountOwner = $p1;
var_dump($a1);
unset($a1);
echo isset($p1) ? '$p1 isset' : '$p1 is not set'; // Person is still set

=============

 

===snippet 02===

$a1 = new Account;
$a1->accountOwner = new Person(10, 'John Doe', '31/07/1967');
var_dump($a1);
unset($a1);
// both Account and Person are unset here

=============

 

In snippet 01 objects Person and Account are created separately and then I reference assign Person to Account, $a1->accountOwner = $p1;

 

In snippet 02 I reference assign Person to Account, $a1->accountOwner = new Person(10, 'John Doe', '31/07/1967'); so that I don't have $p1 explicitly assigned.

 

The doubt is: which one is considered to be Association?

 

Thanx in advance :-)

Link to comment
https://forums.phpfreaks.com/topic/167635-object-association-doubt/
Share on other sites

Well, I do agree that in both cases, we have some object association. I guess the main difference is when we want to unset the objects. In snippet01 we have to unset both explicitly and in snippet02 we just unset one and the other 'dies' along.

 

So basically, choosing between the two alternatives depends on what we want, right?

Well, neither of them is really "storing" the object, but they are storing a reference to the object in memory. Garbage collection should kick in when the last reference to an object is destroyed, so you're right, in snippet 1 the Person object will persist even when the Account object is destroyed. To be honest, there is really no better option. For a regular web page, the automatic garbage collection is typically good enough that you don't have to manually unset variables.

Right, composition denotes ownership of the contained object whereas aggregation does not.

 

Like a university has a composition of departments and the departments have an aggregate of professors and students.

 

Strictly speaking, snippet 1 is aggregation and snippet 2 is composition, but often the distinction doesn't matter.

To ruin your moment of clarity...

 

Strictly speaking, as Daniel explained, compositions means "the whole is composed of its parts" (and, "the whole cannot exist without its parts"). Strictly, neither of the snippets are examples of composition (an Account is not composed of Persons).

 

But often when people say "composition" they mean "life cycle dependency" ("the part cannot exists without the whole", which is an effect of a whole being composed of its parts, but strictly not the same). By this definition snippet 2 could be considered composition. To confuse matters even further, when people say "composition" (or "object composition") even more often they mean aggregation, not composite aggregation. For me personally it helps to district "composition" (as its most widely used interpretation: aggregation) from "composite aggregation" (life cycle dependency). Very little people mean "the whole is composed of its parts" when they say "composition" or "composite aggregation". The difference is purely conceptual in any case.

To ruin your moment of clarity ... etc ... The difference is purely conceptual in any case.

 

... clarity a bit ruined at the moment ... considering that in aggregation if I destroy the whole, its parts will not be destroyed as opposed to composition when the whole destroy its parts, then the difference can't be purely conceptual, right?

 

1. Agreggation: Department and Teacher, destroying the department will not kill the teachers (in some cases it wouldn't be a bad idea, hehe)

 

2. Composition: Readme.txt and "hello world..." (text inside the readme file), destroying Readme.txt will destroy "hello world..."

 

unless I'm not understanding what conceptual means, 1 and 2 have different concepts and they differ in the final results too, let's say 'structure' ...

... clarity a bit ruined at the moment ... considering that in aggregation if I destroy the whole, its parts will not be destroyed as opposed to composition when the whole destroy its parts, then the difference can't be purely conceptual, right?

 

What you are describing is "life cycle dependency". Indeed that is not conceptual.

 

1. Agreggation: Department and Teacher, destroying the department will not kill the teachers (in some cases it wouldn't be a bad idea, hehe)

 

2. Composition: Readme.txt and "hello world..." (text inside the readme file), destroying Readme.txt will destroy "hello world..."

 

Actually both of these can be considered examples of composition, depending where you define the system boundary. If the boundary is the whole world and everything in it, then yes, 1) is aggregation, but if the system boundary is the school, then 1) is composite aggregation as well.

 

unless I'm not understanding what conceptual means, 1 and 2 have different concepts and they differ in the final results too, let's say 'structure' ...

 

What I am trying to explain is that "composition", strictly speaking, is purely conceptual. A conceptual model should dictate whether to apply life cycle dependency. Applying life cycle dependency doesn't make composition, but composition does make life cycle dependency.. Starting to make sense?

 

 

Hi '448191' ... what you're trying to explain to me is that besides the normal definition of what Composition and Aggregation means ... I also have to consider what you call 'system boundary' or let's say the 'scope' where the app as a whole has a meaning and what I want to happen when the whole is destroyed, right?

 

I first said that Teacher and Department was just Aggregation, but according to what you explained, if the system boundary is the school, then 1) is composite aggregation as well, which means that, in this case, the Teacher dies along with the destruction of Department, since in the scope is School (not whole world), destroying the Department, will destroy the School and consequently the Teacher because he/she will not have the School or Department to teach someone ... thus 'composite aggregation' ...

Hi '448191' ... what you're trying to explain to me is that besides the normal definition of what Composition and Aggregation means ... I also have to consider what you call 'system boundary' or let's say the 'scope' where the app as a whole has a meaning and what I want to happen when the whole is destroyed, right?

 

I first said that Teacher and Department was just Aggregation, but according to what you explained, if the system boundary is the school, then 1) is composite aggregation as well, which means that, in this case, the Teacher dies along with the destruction of Department, since in the scope is School (not whole world), destroying the Department, will destroy the School and consequently the Teacher because he/she will not have the School or Department to teach someone ... thus 'composite aggregation' ...

I'm very interested! I would love to find out more inforamtion related to this topic. Thanks in advance.

me too, I need more detailed info

simulation taux banque credit immobilier de France - Credit immobilier de France, simulation credit immobilier. Résultat mitigé pour le crédit immobilier de France.simulation taux banque credit immobilier de France

 

 

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.