Reference or a Copy?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • xu

    Reference or a Copy?

    Hi, i'm quite new with PHP.

    I was wondering if i get a reference or a copy back when i try to
    retrieve it at page2.php $foobar = $_SESSION['myobject'];

    // page1.php

    ....
    $myobject = new MyObject();
    $_SESSION['myobject'] = $myobject;
    ....

    // page2.php

    ....
    $foobar = $_SESSION['myobject'];
    ....
  • neur0maniak

    #2
    Re: Reference or a Copy?

    That would appear to make a copy.

    At the end of page2.php, you could have $_SESSION['myobject']=$foobar;
    to copy it back. Or you could use $_SESSION['myobject'] in place of
    every $foobar and it should work the same.


    xu wrote:[color=blue]
    > Hi, i'm quite new with PHP.
    >
    > I was wondering if i get a reference or a copy back when i try to
    > retrieve it at page2.php $foobar = $_SESSION['myobject'];
    >
    > // page1.php
    >
    > ....
    > $myobject = new MyObject();
    > $_SESSION['myobject'] = $myobject;
    > ....
    >
    > // page2.php
    >
    > ....
    > $foobar = $_SESSION['myobject'];
    > ....[/color]

    Comment

    • Huabi Xu

      #3
      Re: Reference or a Copy?

      Thanks for your reply, it helped me alot with my insight of when php's
      object reference is in place and when not.

      I'm now going to test the & to see if i'm getting a reference or not.

      // page2.php

      ....
      $foobar =& $_SESSION['myobject']; ?
      ....

      neur0maniak wrote:[color=blue]
      > That would appear to make a copy.
      >
      > At the end of page2.php, you could have $_SESSION['myobject']=$foobar;
      > to copy it back. Or you could use $_SESSION['myobject'] in place of
      > every $foobar and it should work the same.
      >
      >
      > xu wrote:
      >[color=green]
      >> Hi, i'm quite new with PHP.
      >>
      >> I was wondering if i get a reference or a copy back when i try to
      >> retrieve it at page2.php $foobar = $_SESSION['myobject'];
      >>
      >> // page1.php
      >>
      >> ....
      >> $myobject = new MyObject();
      >> $_SESSION['myobject'] = $myobject;
      >> ....
      >>
      >> // page2.php
      >>
      >> ....
      >> $foobar = $_SESSION['myobject'];
      >> ....[/color][/color]

      Comment

      Working...