Copying an Object passes Object by Reference?!

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

    #1

    Copying an Object passes Object by Reference?!

    I discovered this by accident today. How should I get an object's
    data into another variable and make modifications without affecting
    the original object?

    // Example of problem

    // here is x, a simple object
    class O {}
    $x = new O;
    $x->name = "old";
    $x->sex = "male";

    O Object
    (
    [name] =old
    [sex] =male
    )
    // moving x to y

    $y = $x;

    // modifying y alters x

    $y->sex = "female";
    $y->name = "new";

    print_r($x);

    O Object
    (
    [name] =new


    [sex] =female
    )
  • Rik Wasmus

    #2
    Re: Copying an Object passes Object by Reference?!

    On Fri, 18 Jan 2008 18:57:19 +0100, jerrygarciuh <jerrygarciuh@g mail.com>
    wrote:
    I discovered this by accident today. How should I get an object's
    data into another variable and make modifications without affecting
    the original object?
    Since PHP5, objects are always passed (& assigned) by reference. Use the
    'clone' keyword if you need a clone, and you can even use the magic
    __clone() method to alter an object on clone (possibly clone objects in
    the properties too etc.).
    --
    Rik Wasmus

    Comment

    • Michael Fesser

      #3
      Re: Copying an Object passes Object by Reference?!

      ..oO(jerrygarci uh)
      >I discovered this by accident today. How should I get an object's
      >data into another variable and make modifications without affecting
      >the original object?
      Clone it:

      $foo = new Object();
      $bar = clone $foo;



      Micha

      Comment

      • jerrygarciuh

        #4
        Re: Copying an Object passes Object by Reference?!

        On Jan 18, 12:14 pm, Michael Fesser <neti...@gmx.de wrote:
        .oO(jerrygarciu h)
        >
        I discovered this by accident today. How should I get an object's
        data into another variable and make modifications without affecting
        the original object?
        >
        Clone it:
        >
        $foo = new Object();
        $bar = clone $foo;
        >

        >
        Micha
        Thanks Micha!

        JG

        Comment

        • jerrygarciuh

          #5
          Re: Copying an Object passes Object by Reference?!

          On Jan 18, 12:13 pm, "Rik Wasmus" <luiheidsgoe... @hotmail.comwro te:
          On Fri, 18 Jan 2008 18:57:19 +0100, jerrygarciuh <jerrygarc...@g mail.com>
          wrote:
          >
          I discovered this by accident today. How should I get an object's
          data into another variable and make modifications without affecting
          the original object?
          >
          Since PHP5, objects are always passed (& assigned) by reference. Use the
          'clone' keyword if you need a clone, and you can even use the magic
          __clone() method to alter an object on clone (possibly clone objects in
          the properties too etc.).
          --
          Rik Wasmus
          Thanks Rik!

          JG


          Comment

          Working...