对象序列化成字符串与反序列化为对象
<?php
class Car {
public $name = 'car';
public function __clone() {
$obj = new Car();
$obj->name = $this->name;
}
}
$a = new Car();
$a->name = 'new car';
$b = clone $a;
if ($a == $b) echo '=='; //true
if ($a === $b) echo '==='; //false 引用
$str = serialize($a); //对象序列化成字符串
echo $str.'<br>';
$c = unserialize($str); //反序列化为对象
class Car {
public $name = 'car';
public function __clone() {
$obj = new Car();
$obj->name = $this->name;
}
}
$a = new Car();
$a->name = 'new car';
$b = clone $a;
if ($a == $b) echo '=='; //true
if ($a === $b) echo '==='; //false 引用
$str = serialize($a); //对象序列化成字符串
echo $str.'<br>';
$c = unserialize($str); //反序列化为对象
var_dump($c);
版权声明:本文为博主原创文章,未经博主允许不得转载。