Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.
$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
$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?
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.
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) ...
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.
$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:
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: