本文主要是介绍php对象串行化,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
<?phpclass User{public $name;public $id;function __construct(){$this->id = uniqid(); //give user a unique ID 赋予一个不同的ID}function __sleep(){ return(array("name")); //do not serialize this->name 不串行化name}function __wakeup(){$this->id = uniqid(); //give 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有不同的IDvar_dump($u); var_dump($u2); ?>
结果如下:
object(User)[1]public 'name' => string 'HAHA' (length=4)public 'id' => string '4e546b245baa1' (length=13)object(User)[2]public 'name' => string 'HAHA' (length=4)public 'id' => string '4e546b245bad1' (length=13)
这篇关于php对象串行化的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!