![]() |
Object Association doubt.
Hi. I was wondering which one of the samples below would be right to set as object association.
===snippet 01=== PHP Code:
===snippet 02=== PHP Code:
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 :-) |
Neither of those are passing a variable by reference.
In snippet 1, $p1 is being copied into $a1->accountOwner. Both variables have their own location within the memory. Put an & in front of the variable and both variables will be using the same location in memory making $a1->accountOwner and $p1 essentially the same whereas before they were two different objects in memory.PHP Code:
|
Eric, for PHP 5 and onwards that's simply not true. Objects are assigned by reference; if you wish to assign a copy then use the clone keyword.
|
Yes, we don't need the & in PHP5 ... but actually snippet 01 and 02 are not regular Association, but instead the specialized versions: Agregattion and Composition
|
| All times are GMT. The time now is 05:31 AM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0