The explanation of serialize() and unserialize() in the PHP manual is:
serialize — Generates a storable representation of a value, generates a storable representation of a value.
unserialize — Creates a PHP value from a stored representation, creates a PHP value from a stored representation.
serialize() returns a string, which contains a byte stream representing value and can be stored anywhere. This facilitates storing or passing PHP values without losing their type and structure.
To convert a serialized string back into a PHP value, use unserialize() . serialize() can handle any type except resource. You can even serialize() arrays that contain references to themselves. References in the array/object you are serializing() will also be stored.
When serializing an object, PHP will attempt to call the object's member function __sleep() before the sequence action. This allows any cleanup operations to be done before the object is serialized. Similarly, when an object is restored using unserialize(), the __wakeup() member function will be called.
Let’s try how to use this function:
<?php //声明一个类 class dog { var $name; var $age; var $owner; function dog($in_name="unnamed", $in_age="0", $in_owner="unknown") { $this->name = $in_name; $this->age = $in_age; $this->owner = $in_owner; } function getage() { return ($this->age * 365); } function getowner() { return ($this->owner); } function getname() { return ($this->name); } } //实例化这个类 $ourfirstdog = new dog("Rover",12,"Lisa and Graham"); //用serialize函数将这个实例转化为一个序列化的字符串 $dogdisc = serialize($ourfirstdog); print $dogdisc; //$ourfirstdog 已经序列化为字符串 O:3:"dog":3:{s:4:"name";s:5:"Rover";s:3:"age";i:12;s:5:"owner";s:15:"Lisa and Graham";} print '<br />'; /* ----------------------------------------------------------------------------------------- 在这里你可以将字符串 $dogdisc 存储到任何地方如 session,cookie,数据库,php文件 ----------------------------------------------------------------------------------------- */ //我们在此注销这个类 unset($ourfirstdog); /* 还原操作 */ /* ----------------------------------------------------------------------------------------- 在这里将字符串 $dogdisc 从你存储的地方读出来如 session,cookie,数据库,php文件 ----------------------------------------------------------------------------------------- */ //我们在这里用 unserialize() 还原已经序列化的对象 $pet = unserialize($dogdisc); //此时的 $pet 已经是前面的 $ourfirstdog 对象了 //获得年龄和名字属性 $old = $pet->getage(); $name = $pet->getname(); //这个类此时无需实例化可以继续使用,而且属性和值都是保持在序列化之前的状态 print "Our first dog is called $name and is $old days old<br />"; print '<br />'; ?>
An official program example:
<?php // $session_data 是包含了当前用户 session 信息的多维数组。 // 我们使用 serialize() 在请求结束之前将其存储到数据库中。 $conn = odbc_connect ( "webdb" , "php" , "chicken" ); $stmt = odbc_prepare ( $conn , "UPDATE sessions SET data = ? WHERE id = ?" ); $sqldata = array ( serialize ( $session_data ), $PHP_AUTH_USER ); if (! odbc_execute ( $stmt , & $sqldata )) { $stmt = odbc_prepare ( $conn , "INSERT INTO sessions (id, data) VALUES(?, ?)" ); if (! odbc_execute ( $stmt , & $sqldata )) { /* 出错 */ } } ?>