Page 1 of 1
Object Association doubt.
Posted: Mon Jul 27, 2009 12:22 pm
by sohdubom
Hi. I was wondering which one of the samples below would be right to set as object association.
===snippet 01===
Code: Select all
$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===
Code: Select all
$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

Re: Object Association doubt.
Posted: Mon Jul 27, 2009 6:25 pm
by Christopher
I am not sure what you mean by "Association", but those two pieces of code result in exactly the same thing. Objects are passed/assigned by handle in PHP so it is always the same object.
Re: Object Association doubt.
Posted: Mon Jul 27, 2009 7:23 pm
by sohdubom
Considering we have 3 basic association types: Association(aka, regular Association), Aggregation and Composition ... after some more thinking I just realised the code above is NOT regular Association, but instead it'a a case of Aggregation and Composition (01 and 02) ...

Re: Object Association doubt.
Posted: Mon Jul 27, 2009 8:21 pm
by Christopher
sohdubom wrote:Considering we have 3 basic association types: Association(aka, regular Association), Aggregation and Composition ... after some more thinking I just realised the code above is NOT regular Association, but instead it'a a case of Aggregation and Composition (01 and 02) ...

I think perhaps you are mixing the reference/handle counter functionality and the memory management in PHP, for the UML terms and how it might work in a manually memory managed system like C. The associations you are talking about have more to do with which object controls which object and the object's lifecycles. The concepts are similar in PHP, but not exactly the same that long-lived programs in other languages. Remember, all of these objects are destroyed microseconds later.
Re: Object Association doubt.
Posted: Tue Jul 28, 2009 1:05 am
by Darhazer
sohdubom wrote:Hi. I was wondering which one of the samples below would be right to set as object association.
===snippet 01===
Code: Select all
$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
=============
Actually you've never unset the $p1 variable, so it's working in the way it's meant to.
More interesting example is if you unset($p1) - you'll still have the object in the accountOwner property.
When you are performing unset(), PHP checks for any references to that variable, and makes a copy of the variable, so it keeps the value of the references. So:
Code: Select all
$a = 5;
$b = & $a;
unset($a);
var_dump($b); // still 5
$a = 5;
$b = & $a;
$a = null;
var_dump($b); // null
P.S. I think even in manual-memory-management environment like C/C++, if you don't delete accountOwner in the destructor, the p1 will exist.
Re: Object Association doubt.
Posted: Tue Jul 28, 2009 8:04 am
by sohdubom
Darhazer wrote:Actually you've never unset the $p1 variable, so it's working in the way it's meant to. More interesting example is if you unset($p1) - you'll still have the object in the accountOwner property. When you are performing unset(), PHP checks for any references to that variable, and makes a copy of the variable, so it keeps the value of the references. So:
Code: Select all
$a = 5;
$b = & $a;
unset($a);
var_dump($b); // still 5
$a = 5;
$b = & $a;
$a = null;
var_dump($b); // null
Great input, actually it's very interesting to see the difference between : unset and NULL