note 111041 deleted from language.oop5.cloning by danbrown
| From: | danbrown@php.net | Date: | Thu, 17 Jan 2013 15:18:15 +0000 |
| Subject: | note 111041 deleted from language.oop5.cloning by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-193172@lists.php.net to get a copy of this message | ||
Note Submitter: nikoolin at ukr dot net
----
Remark for "Arrays are shallow cloned on assignment, so don't use the clone keyword on
them, just assign it to a new variable. That would lead to an error instead."
Following code works well:
$c = [1,2,3];
$d = [4,5,6];
$a = new StdClass();
$a->arr = $c;
$b = clone $a;
$a->arr = $d;
var_dump($b);
var_dump($a);
Retursn:
object(stdClass)#2 (1) { ["arr"]=> array(3) { [0]=> int(1) [1]=> int(2) [2]=>
int(3) } }
object(stdClass)#1 (1) { ["arr"]=> array(3) { [0]=> int(4) [1]=> int(5) [2]=>
int(6) } }