第十三節--對象串行化 <?php class User { public $name; public $id; function __construct() { //give user a unique ID 賦予一個不同的ID $this->id = uniqid(); } function __sleep() { //do not serialize this->id 不串行化id return(array("name")); } function __wakeup() { //give user a unique ID $this->id = uniqid(); } } //create object 建立一個對象 $u = new User; $u->name = "Leon"; //serialize it 串行化 注意不串行化id屬性,id的值被拋棄 $s = serialize($u); //unserialize it 反串行化 id被重新賦值 $u2 = unserialize($s); //$u and $u2 have different IDs $u和$u2有不同的ID print_r($u); print_r($u2); ?> |
溫馨提示:喜歡本站的話,請收藏一下本站!