Arrays always use references?

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

    Arrays always use references?

    I have the following code:

    $Foo = new CFoo();
    $Foo->Bar = 5;

    $_SESSION['foo'] = $Foo;
    $_SESSION['bar'] = $Foo->Bar;

    $Foo->Bar = 10;

    echo $Foo->Bar;
    echo $_SESSION['foo']->Bar;
    echo $_SESSION['bar'];

    outputs
    10
    10
    10

    How can I force an array to contain an instance rather than a
    reference, so for instance $_SESSION['bar'] is still equal to 5?

    Thanks

  • Alvaro G. Vicario

    #2
    Re: Arrays always use references?

    *** lister escribió/wrote (12 Apr 2007 07:53:54 -0700):
    $Foo = new CFoo();
    $Foo->Bar = 5;
    >
    $_SESSION['foo'] = $Foo;
    $_SESSION['bar'] = $Foo->Bar;
    >
    $Foo->Bar = 10;
    >
    echo $Foo->Bar;
    echo $_SESSION['foo']->Bar;
    echo $_SESSION['bar'];
    >
    outputs
    10
    10
    10
    In my system, this code prints:

    10
    10
    5

    A wild guess... register_global s set to on?

    --
    -+ http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
    ++ Mi sitio sobre programación web: http://bits.demogracia.com
    +- Mi web de humor con rayos UVA: http://www.demogracia.com
    --

    Comment

    • lister

      #3
      Re: Arrays always use references?

      On Apr 12, 6:53 pm, "Alvaro G. Vicario"
      <webmas...@NOSP AMdemogracia.co mwrote:
      *** lister escribió/wrote (12 Apr 2007 07:53:54 -0700):
      >
      >
      >
      >
      >
      $Foo = new CFoo();
      $Foo->Bar = 5;
      >
      $_SESSION['foo'] = $Foo;
      $_SESSION['bar'] = $Foo->Bar;
      >
      $Foo->Bar = 10;
      >
      echo $Foo->Bar;
      echo $_SESSION['foo']->Bar;
      echo $_SESSION['bar'];
      >
      outputs
      10
      10
      10
      >
      In my system, this code prints:
      >
      10
      10
      5
      >
      A wild guess... register_global s set to on?
      >
      --
      -+http://alvaro.es- Álvaro G. Vicario - Burgos, Spain
      ++ Mi sitio sobre programación web:http://bits.demogracia.com
      +- Mi web de humor con rayos UVA:http://www.demogracia.com
      --- Hide quoted text -
      >
      - Show quoted text -
      Many thanks for your reply, but I think I've just realised what is
      going on.
      Bar is actually another object in my real code, and I've just read
      that all objects are references. D'oh.
      I have also found the "clone" command. <slinks away>

      Comment

      • Henk verhoeven

        #4
        Re: Arrays always use references?

        Maybe you are using php4 where lister uses php5?

        Alvaro G. Vicario schreef:
        *** lister escribió/wrote (12 Apr 2007 07:53:54 -0700):
        >$Foo = new CFoo();
        >$Foo->Bar = 5;
        >>
        >$_SESSION['foo'] = $Foo;
        >$_SESSION['bar'] = $Foo->Bar;
        >>
        >$Foo->Bar = 10;
        >>
        >echo $Foo->Bar;
        >echo $_SESSION['foo']->Bar;
        >echo $_SESSION['bar'];
        >>
        >outputs
        >10
        >10
        >10
        >
        In my system, this code prints:
        >
        10
        10
        5
        >
        A wild guess... register_global s set to on?
        >

        Comment

        Working...