$a = ['a'=>array('b'=>1)];
var_dump($a);
/$b = $a;
$b['a'] = 2;
var_dump($b);
var_dump($a);
两次输出的$a 一致 说明默认的赋值过程是深度拷贝调用
class A {
public $a = array('a'=>'2');
};
$a = new A();
var_dump($a);
$b = $a;
$b->a = 3;
var_dump($a);
var_dump($b);
输出:
object(A)#1 (1) {
["a"]=>
array(1) {
["a"]=>
string(1) "2"
}
}
object(A)#1 (1) {
["a"]=>
int(3)
}
object(A)#1 (1) {
["a"]=>
int(3)
}
可见对象默认的赋值并非深度拷贝 {
["a"]=>
array(1) {
["a"]=>
string(1) "2"
}
}
object(A)#1 (1) {
["a"]=>
int(3)
}
object(A)#1 (1) {
["a"]=>
int(3)
}。。浅拷贝
3 使用clone拷贝对象(深度拷贝 说明默认的对象__clone魔术方法实现)
class A {
public $a = array('a'=>'2');
};
$a = new A();
var_dump($a);
$b = clone $a;
$b->a = 3;
var_dump($a);
var_dump($b);
输出:
object(A)#1 (1) { ["a"]=> array(1) { ["a"]=> string(1) "2" } } object(A)#1 (1) { ["a"]=> array(1) { ["a"]=> string(1) "2" } } object(A)#2 (1) { ["a"]=> int(3) }