Object references... i simply don't understand it at all ;)

This is a discussion on Object references... i simply don't understand it at all ;) within the PHP Language forums, part of the PHP Programming Forums category; Hello! I can't see any difference between: $MyObject = new Object(); and $MyObject =& new Object(); I used to think ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 08-10-2004
kajaman (a.k.a. Hubert Łępicki ;))
 
Posts: n/a
Default Object references... i simply don't understand it at all ;)

Hello!

I can't see any difference between:

$MyObject = new Object();
and
$MyObject =& new Object();

I used to think that if I use =&, I will be able to change variables of
the class and each new object created after that from the modified class
will have new, modified variables - but that seems not to be true...

I use PHP5 - is it the problem?
Reply With Quote
  #2 (permalink)  
Old 08-10-2004
rush
 
Posts: n/a
Default Re: Object references... i simply don't understand it at all ;)

"kajaman (a.k.a. Hubert ŁTpicki ;))" <hubert5@wp.pl> wrote in message
news:cfadvk$nkg$1@nemesis.news.tpi.pl...
> $MyObject = new Object();
> and
> $MyObject =& new Object();
>
> I used to think that if I use =&, I will be able to change variables of
> the class and each new object created after that from the modified class
> will have new, modified variables - but that seems not to be true...


no if you add:

$AnotherMyObject = & $MyObject;

than changing $AnotherObject will also change $MyObject and other way
around.

rush
--

http://www.templatetamer.com/


Reply With Quote
  #3 (permalink)  
Old 08-10-2004
kajaman (a.k.a. Hubert Łępicki ;))
 
Posts: n/a
Default Re: Object references... i simply don't understand it at all ;)

rush wrote:
>
> $AnotherMyObject = & $MyObject;
>
> than changing $AnotherObject will also change $MyObject and other way
> around.


Yes! And I understand it! But... Why people use:
$MyObject =& new Object
while it seems to work exacly the same as:
$MyObject = new Object...
strange :-/
Reply With Quote
  #4 (permalink)  
Old 08-10-2004
Matthias Esken
 
Posts: n/a
Default Re: Object references... i simply don't understand it at all ;)

kajaman (a.k.a. Hubert ??picki ;)) schrieb:

> Yes! And I understand it! But... Why people use:
> $MyObject =& new Object
> while it seems to work exacly the same as:
> $MyObject = new Object...


It's the same in PHP5.

In PHP4 it is different:

$MyObject = &new Object;
creates a new instance and $MyObject get's a reference to it.

$MyObject = new Object;
creates a new instance and copies it to $MyObject.

Regards,
Matthias
Reply With Quote
  #5 (permalink)  
Old 08-10-2004
Kevin Thorpe
 
Posts: n/a
Default Re: Object references... i simply don't understand it at all ;)

kajaman (a.k.a. Hubert Łępicki ;)) wrote:

> rush wrote:
>
>>
>> $AnotherMyObject = & $MyObject;
>>
>> than changing $AnotherObject will also change $MyObject and other way
>> around.

>
>
> Yes! And I understand it! But... Why people use:
> $MyObject =& new Object


PHP4: creates a new Object then points $MyObject at it

> while it seems to work exacly the same as:
> $MyObject = new Object...


PHP4: creates a new Object then copies the contents of same into
$MyObject (MyObject is a copy)

In practical terms you end up with the same result. However you have a
spare object floating around to be cleaned up by the garbage collector.
Reply With Quote
  #6 (permalink)  
Old 08-10-2004
kajaman (a.k.a. Hubert Łępicki ;))
 
Posts: n/a
Default Re: Object references... i simply don't understand it at all ;)

Kevin Thorpe wrote:
> kajaman (a.k.a. Hubert Łępicki ;)) wrote:
>
>> rush wrote:
>>
>>>
>>> $AnotherMyObject = & $MyObject;
>>>
>>> than changing $AnotherObject will also change $MyObject and other way
>>> around.

>>
>>
>>
>> Yes! And I understand it! But... Why people use:
>> $MyObject =& new Object

>
>
> PHP4: creates a new Object then points $MyObject at it
>
>> while it seems to work exacly the same as:
>> $MyObject = new Object...

>
>
> PHP4: creates a new Object then copies the contents of same into
> $MyObject (MyObject is a copy)
>
> In practical terms you end up with the same result. However you have a
> spare object floating around to be cleaned up by the garbage collector.

Thank you very much, now I see it clear :).
Reply With Quote
  #7 (permalink)  
Old 08-10-2004
Michael Fesser
 
Posts: n/a
Default Re: Object references... i simply don't understand it at all ;)

.oO(kajaman (a.k.a. Hubert ??picki ;)))

>Yes! And I understand it! But... Why people use:
>$MyObject =& new Object
>while it seems to work exacly the same as:
>$MyObject = new Object...


Only in PHP5, it uses references by default. In PHP4 it's a difference.

Micha
Reply With Quote
Reply
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

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




All times are GMT +1. The time now is 08:53 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0