Object Association doubt.

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.

Moderator: General Moderators

Post Reply
sohdubom
Forum Newbie
Posts: 3
Joined: Sat Mar 14, 2009 1:45 pm

Object Association doubt.

Post 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 :-)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Object Association doubt.

Post 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.
(#10850)
sohdubom
Forum Newbie
Posts: 3
Joined: Sat Mar 14, 2009 1:45 pm

Re: Object Association doubt.

Post 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) ... :-)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Object Association doubt.

Post 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.
(#10850)
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Object Association doubt.

Post 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.
sohdubom
Forum Newbie
Posts: 3
Joined: Sat Mar 14, 2009 1:45 pm

Re: Object Association doubt.

Post 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
Post Reply