class="article_content clearfix">
class="htmledit_views">
class="language-class="tags" href="/tags/PHP.html" title=php>php"><?class="tags" href="/tags/PHP.html" title=php>php
class User
{
public $name;
public $id;
class="tags" href="/tags/FUNCTION.html" title=function>function __construct()
{
$this->id = uniqid(); //give class="tags" href="/tags/USER.html" title=user>user a unique ID 赋予一个不同的ID
}
class="tags" href="/tags/FUNCTION.html" title=function>function __sleep()
{
return(array("name")); //do not serialize this->name 不串行化name
}
class="tags" href="/tags/FUNCTION.html" title=function>function __wakeup()
{
$this->id = uniqid(); //give class="tags" href="/tags/USER.html" title=user>user a unique ID,id在反序列化时,被重新赋值
}
}
$u = new User;
$u->name = "HAHA";
$s = serialize($u); //serialize it 串行化 注意不串行化id属性,id的值被抛弃
$u2 = unserialize($s); //unserialize it 反串行化 id被重新赋值
//$u and $u2 have different IDs $u和$u2有不同的ID
var_dump($u);
var_dump($u2);
?>
结果如下:
class="language-html">class="tags" href="/tags/OBJECT.html" title=object>object(User)[1]
public 'name' => class="tags" href="/tags/STRING.html" title=string>string 'HAHA' (length=4)
public 'id' => class="tags" href="/tags/STRING.html" title=string>string '4e546b245baa1' (length=13)
class="tags" href="/tags/OBJECT.html" title=object>object(User)[2]
public 'name' => class="tags" href="/tags/STRING.html" title=string>string 'HAHA' (length=4)
public 'id' => class="tags" href="/tags/STRING.html" title=string>string '4e546b245bad1' (length=13)