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 ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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? |
|
|||
|
"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/ |
|
|||
|
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 :-/ |
|
|||
|
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 |
|
|||
|
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. |
|
|||
|
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 :). |
|
|||
|
.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 |