TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 07-27-2009, 04:58 PM   #1 (permalink)
The Wanderer
 
Join Date: Jul 2009
Posts: 5
Thanks: 0
sohdubom is on a distinguished road
Default 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:
$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===
PHP Code:
$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
sohdubom is offline  
Reply With Quote
Old 07-27-2009, 08:13 PM   #2 (permalink)
The Acquainted
 
wGEric's Avatar
 
Join Date: Nov 2007
Posts: 166
Thanks: 0
wGEric is on a distinguished road
Default

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:
$p1->name 'John';
$a1->accountOwner = &$p1;

$a1->accountOwner->name 'Bob';
echo 
$p1->name// returns 'Bob' 
__________________
Eric
wGEric is offline  
Reply With Quote
Old 07-27-2009, 08:17 PM   #3 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

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.
Salathe is offline  
Reply With Quote
Old 07-27-2009, 08:41 PM   #4 (permalink)
The Wanderer
 
Join Date: Jul 2009
Posts: 5
Thanks: 0
sohdubom is on a distinguished road
Default

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
sohdubom is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Using the Clone Construct to Clone an Object Wildhoney Advanced PHP Programming 5 01-29-2013 11:51 AM
Using the factory pattern (mad rantings of a mind without coffee) sketchMedia Advanced PHP Programming 35 09-25-2009 11:05 AM
Object Aggregation Jenski Advanced PHP Programming 2 08-07-2008 09:53 AM
array to object Gibou Advanced PHP Programming 11 03-24-2008 06:07 PM
Object methods suddenly lagging? RobertK Advanced PHP Programming 9 01-11-2008 01:38 PM


All times are GMT. The time now is 09:02 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design