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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kajaman (a.k.a. Hubert Łępicki ;))

    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?
  • rush

    #2
    Re: Object references... i simply don't understand it at all ;)

    "kajaman (a.k.a. Hubert ŁÄTpicki ;))" <hubert5@wp.p l> wrote in message
    news:cfadvk$nkg $1@nemesis.news .tpi.pl...[color=blue]
    > $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...[/color]

    no if you add:

    $AnotherMyObjec t = & $MyObject;

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

    rush
    --




    Comment

    • kajaman (a.k.a. Hubert Łępicki ;))

      #3
      Re: Object references... i simply don't understand it at all ;)

      rush wrote:[color=blue]
      >
      > $AnotherMyObjec t = & $MyObject;
      >
      > than changing $AnotherObject will also change $MyObject and other way
      > around.[/color]

      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 :-/

      Comment

      • Michael Fesser

        #4
        Re: Object references... i simply don't understand it at all ;)

        .oO(kajaman (a.k.a. Hubert ??picki ;)))
        [color=blue]
        >Yes! And I understand it! But... Why people use:
        >$MyObject =& new Object
        >while it seems to work exacly the same as:
        >$MyObject = new Object...[/color]

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

        Micha

        Comment

        • Kevin Thorpe

          #5
          Re: Object references... i simply don't understand it at all ;)

          kajaman (a.k.a. Hubert Łępicki ;)) wrote:
          [color=blue]
          > rush wrote:
          >[color=green]
          >>
          >> $AnotherMyObjec t = & $MyObject;
          >>
          >> than changing $AnotherObject will also change $MyObject and other way
          >> around.[/color]
          >
          >
          > Yes! And I understand it! But... Why people use:
          > $MyObject =& new Object[/color]

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

          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.

          Comment

          • Matthias Esken

            #6
            Re: Object references... i simply don't understand it at all ;)

            kajaman (a.k.a. Hubert ??picki ;)) schrieb:
            [color=blue]
            > Yes! And I understand it! But... Why people use:
            > $MyObject =& new Object
            > while it seems to work exacly the same as:
            > $MyObject = new Object...[/color]

            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

            Comment

            • kajaman (a.k.a. Hubert Łępicki ;))

              #7
              Re: Object references... i simply don't understand it at all ;)

              Kevin Thorpe wrote:[color=blue]
              > kajaman (a.k.a. Hubert Łępicki ;)) wrote:
              >[color=green]
              >> rush wrote:
              >>[color=darkred]
              >>>
              >>> $AnotherMyObjec t = & $MyObject;
              >>>
              >>> than changing $AnotherObject will also change $MyObject and other way
              >>> around.[/color]
              >>
              >>
              >>
              >> Yes! And I understand it! But... Why people use:
              >> $MyObject =& new Object[/color]
              >
              >
              > PHP4: creates a new Object then points $MyObject at it
              >[color=green]
              >> while it seems to work exacly the same as:
              >> $MyObject = new Object...[/color]
              >
              >
              > 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.[/color]
              Thank you very much, now I see it clear :).

              Comment

              Working...