|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 ) |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
On Fri, 18 Jan 2008 18:57:19 +0100, jerrygarciuh <jerrygarciuh@gmail.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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
..oO(jerrygarciuh)
>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; http://www.php.net/manual/en/language.oop5.cloning.php Micha |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
On Jan 18, 12:14 pm, Michael Fesser <neti...@gmx.de> wrote:
> .oO(jerrygarciuh) > > >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; > > http://www.php.net/manual/en/language.oop5.cloning.php > > Micha Thanks Micha! JG |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
On Jan 18, 12:13 pm, "Rik Wasmus" <luiheidsgoe...@hotmail.com> wrote:
> On Fri, 18 Jan 2008 18:57:19 +0100, jerrygarciuh <jerrygarc...@gmail.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 |
|
![]() |
| Outils de la discussion | |
|
|