在PHP中的普通变量(除对象外)的赋值,是"传值", 可以直接复制出新的变量;但对象变量是"传址"(引用), 必须使用clone(浅复制)或序列化(深复制)。
$str_a = 'aaa';
$str_b = $str_a; //一个新的字符串变量
$str_a = 'ccc';
var_dump($str_a, $str_b);//string(3) "ccc" string(3) "aaa"
$int_a = 10;
$int_b = $int_a; //int_b是一个新的整型变量
$int_a = 20;
var_dump($int_a, $int_b);//int(20) int(10)
$arr_a = ['a', 'b', 'c'];
$arr_b = $arr_a;// arr_b是一个新的数组
$arr_a = ['e', 'f', 'g'];
var_dump($arr_a, $arr_b);// ['e', 'f', 'g'] ['a', 'b', 'c']
如果像普通变量一样去"复制"对象,例如:
class Person{
public $gender;//性别
function __construct($gender){
$this->gender = $gender;
}
}
$Person1 = new Person('male');
$Person2 = $Person1;
$Person3 = &$Person1;
$Person1->gender='female';
echo $Person2->gender;//female
echo $Person3->gender;//female
上面是像普通变量一样直接使用 “=” 去 "复制" 对象, , 其实都是对原对象的”引用“, 而这不是我们要的结果。
So, PHP提供了一个clone方法。
$Person1 = new Person('male');
$Person2 = clone $Person1;
$Person1->gender='female';
echo $Person2->gender;//male
这只是一个"浅复制"。如果对象的属性里面也有对象类型的话,也会出现之前的bug, 这时我们需要在魔术方法__clone里面再次去clone对象属性:
class Education{
public $school_name;//学校名称
public $enroll_date;//入学时间
public $graduate_date;//毕业时间
public $major;//专业名称
}
class Person{
public $gender;//性别
public $education;//受教育情况
function __construct($gender, Education $education){
$this->gender = $gender;
$this->education = $education;
}
function __clone(){
$this->education = clone $this->education;
}
}
$Person1 = new Person('male', new Education());
$Person2 = clone $Person1;
如果属性中的对象类型有多个, 就需要clone多次。而且还可能会出现"嵌套clone"的需求。
So, 我们需要用到"序列化":
$Person2 = unserialize(serialize($Person1));
序列化是一个递归的过程,不需要关心对象内部引用了多少个对象或者引用了多少层对象,序列化都会帮我们处理好。
在序列化与反序列化操作过程中, 会调用类的__serialize,__unserialize魔术方法(PHP7.4版本以下是: __sleep,__wakeup)。
但是序列化的不足之处在于:无法触发__clone魔术方法。如果需要在clone操作时修改某些属性,则需要再次clone操作来触发。