# clone wars

- Thread: https://externals.io/message/7851
- Original archive: https://news-web.php.net/php.internals/7851
- Started by Herr Witten on 2004-02-15T22:45:56+00:00
- Messages: 12
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #7851 — Herr Witten — 2004-02-15T22:45:56+00:00

URL: https://externals.io/message/7851#7851

It appears that cloning is broken, since a modification to the clone 
modifies the original. For my __clone method implementation, I allow 
all of the variables to be set by default except for one. But this 
problem persists even when I don't modify that one variable.

---

## #7852 — Herr Witten — 2004-02-15T22:46:24+00:00

URL: https://externals.io/message/7851#7852 · In reply to: #7851

I forgot to mention: I am use b4



> It appears that cloning is broken, since a modification to the clone 
> modifies the original. For my __clone method implementation, I allow 
> all of the variables to be set by default except for one. But this 
> problem persists even when I don't modify that one variable.

---

## #7853 — Marcus Boerger — 2004-02-15T22:47:59+00:00

URL: https://externals.io/message/7851#7853 · In reply to: #7851

Hello,

most probably that property is a reference in which case the result is
correct.

Sunday, February 15, 2004, 11:45:56 PM, you wrote:

> It appears that cloning is broken, since a modification to the clone 
> modifies the original. For my __clone method implementation, I allow 
> all of the variables to be set by default except for one. But this 
> problem persists even when I don't modify that one variable.




-- 
Best regards,
 Marcus                            mailto:helly@php.net

---

## #7854 — Herr Witten — 2004-02-16T00:35:07+00:00

URL: https://externals.io/message/7851#7854 · In reply to: #7853

It appears that only one of the variables, an array, is being shared 
across these multiple objects, but other array variables are not. 
Moreover, When I make a clone and then set the clone to null, all 
clones made thereafter are correct.

$clone = clone $object;
$clone = null;

Also, I can't replicate the problem with another class. It only seems 
to be this class.



> Hello,
>
> most probably that property is a reference in which case the result is
> correct.
>
> Sunday, February 15, 2004, 11:45:56 PM, you wrote:
>
> > […]
>
>
>
>
> -- 
> Best regards,
>  Marcus                            mailto:helly@php.net

---

## #7855 — Herr Witten — 2004-02-16T00:39:44+00:00

URL: https://externals.io/message/7851#7855 · In reply to: #7854

More specifically,

This causes a change in $object:

$clone = clone $object;
$clone->troublesomeVariable = array();

But this doesn't:

$clone = clone $cell;
$clone = null;
$clone->_subElements = array();



> It appears that only one of the variables, an array, is being shared 
> across these multiple objects, but other array variables are not. 
> Moreover, When I make a clone and then set the clone to null, all 
> clones made thereafter are correct.
>
> $clone = clone $object;
> $clone = null;
>
> Also, I can't replicate the problem with another class. It only seems 
> to be this class.
>
> 
>
> > […]

---

## #7856 — Herr Witten — 2004-02-16T00:43:59+00:00

URL: https://externals.io/message/7851#7856 · In reply to: #7855

That should be clone $object for the example.



> $clone = clone $cell;

---

## #7857 — Marcus Boerger — 2004-02-16T00:45:53+00:00

URL: https://externals.io/message/7851#7857 · In reply to: #7855

Hello,

first as i said do the following to verify:
marcus@frodo /usr/src/php5 $ php -r '$a=array(1,2);class t{public $a;function f(){$this->a=&$GLOBALS["a"];}}$o1=new t;$o1->f();var_dump($o1);$o2=clone $o1;$o1->a=2;var_dump($o2);var_dump($o1);'
make: `sapi/cli/php' is up to date.
object(t)#1 (1) {
  ["a"]=>
  &array(2) {
    [0]=>
    int(1)
    [1]=>
    int(2)
  }
}
object(t)#2 (1) {
  ["a"]=>
  &int(2)
}
object(t)#1 (1) {
  ["a"]=>
  &int(2)
}
then remove the '&'.

second read the docs and question such things on general@php.net.

Monday, February 16, 2004, 1:39:44 AM, you wrote:

> More specifically,

> This causes a change in $object:

> $clone = clone $object;
> $clone->troublesomeVariable = array();

> But this doesn't:

> $clone = clone $cell;
> $clone = null;
> $clone->_subElements = array();

> 

> > […]




-- 
Best regards,
 Marcus                            mailto:helly@php.net

---

## #7858 — Herr Witten — 2004-02-16T00:54:36+00:00

URL: https://externals.io/message/7851#7858 · In reply to: #7857

> first as i said do the following to verify:

It's a nice example, but it doesn't apply. I don't make a reference.

> second read the docs and question such things on general@php.net.

I did. This problem seemed to be internal, and it still does.

---

## #7859 — Marcus Boerger — 2004-02-16T01:04:53+00:00

URL: https://externals.io/message/7851#7859 · In reply to: #7858

Hello,

Monday, February 16, 2004, 1:54:36 AM, you wrote:

> > […]

> It's a nice example, but it doesn't apply. I don't make a reference.

> > […]

> I did. This problem seemed to be internal, and it still does.


Ok, so you are sure `var_dump()` doesn't show the & for the property in
question? If so i would need to know where exactly the array comes from.
And the best way to proceed tehn is to create a bug report with a short
reproducing script.

-- 
Best regards,
 Marcus                            mailto:helly@php.net

---

## #7860 — Herr Witten — 2004-02-16T02:14:16+00:00

URL: https://externals.io/message/7851#7860 · In reply to: #7859

Here, I have reproduced the problem:

class A
{
     var $a = array();

     public function makeAReference()
     {
         $array = $this->getA();
     }

     public function &getA()
     {
         return $this->a;
     }
}

$A = new A;
$A->a = array(1);
$A->makeAReference();
$clone = clone $A;
$clone->a = array();

print_r($A);

Try it with an without $A->makeAReference()

I sent in a report. Thanks.



> Ok, so you are sure `var_dump()` doesn't show the & for the property in
> question? If so i would need to know where exactly the array comes 
> from.
> And the best way to proceed tehn is to create a bug report with a short
> reproducing script.

---

## #7861 — Herr Witten — 2004-02-16T04:57:11+00:00

URL: https://externals.io/message/7851#7861 · In reply to: #7860

This too:

class A
{
     var $a = array();

     public function &getA()
     {
         return $this->a;
     }
}

$A = new A;
$A->a = array(1);
$array = &$A->getA();
$clone = clone $A;
$clone->a = array();

print_r($A);



> Here, I have reproduced the problem:
>
> class A
> {
>     var $a = array();
>
>     public function makeAReference()
>     {
>         $array = $this->getA();
>     }
>
>     public function &getA()
>     {
>         return $this->a;
>     }
> }
>
> $A = new A;
> $A->a = array(1);
> $A->makeAReference();
> $clone = clone $A;
> $clone->a = array();
>
> print_r($A);
>
> Try it with an without $A->makeAReference()
>
> I sent in a report. Thanks.

---

## #8049 — Herr Witten — 2004-02-22T15:31:34+00:00

URL: https://externals.io/message/7851#8049 · In reply to: #7861

This was never resolved:

Is this, or is this not a bug:

class A
{
     var $a;
}

$A = new A;
$A->a = array(1);
$array = &$A->a;
$clone = clone $A;
$clone->a = array(); //Affects a in $A.

print_r($A);
