We all know that PHP serialization can convert variables, including objects, into continuous bytes data. You can Store the serialized variables in a file or transmit them over the network, and then deserialize them back to the original data. This article gives you a detailed introduction to PHP serialization. PHP can successfully store the properties and methods of a class that you define before deserializing its object. Sometimes you may need an object to be executed immediately after deserializing it. For such purposes, PHP automatically looks for the __sleep and __wakeup methods.
When an object is serialized by PHP, PHP will call the __sleep method (if it exists). After deserializing an object, PHP will call the __wakeup method. Neither method accepts parameters. __sleep method Must return an array containing the attributes that need to be serialized. PHP will discard the values of other attributes. Without the __sleep method, PHP will save all attributes.
<?<span>php </span><span>/*</span><span> * * @Authors peng--jun * @Email 1098325951@qq.com * @Date 2016-01-23 14:40:38 * @Link </span><span>http://www.cnblogs.com/xs-yqz/</span><span> * @version $Id$ ========================================== </span><span>*/</span><span> header(</span><span>"</span><span>Content-type: text/html; charset=UTF-8</span><span>"</span><span>); </span><span>class</span><span> Person{ </span><span>private</span><span> $name; </span><span>private</span><span> $sex; </span><span>private</span><span> $age; function __construct($name,$age,$sex){ $</span><span>this</span>->name =<span> $name; $</span><span>this</span>->age =<span> $age; $</span><span>this</span>->sex =<span> $sex; } function say(){ echo </span><span>"</span><span>我的名字:</span><span>"</span>.$<span>this</span>->name.<span>"</span><span>性别为: </span><span>"</span>.$<span>this</span>->sex.<span>"</span><span>年龄为:</span><span>"</span>.$<span>this</span>-><span>age; } <span>//在类中添加此方法,在串行化的时候自动调用并返回数组</span> function __sleep(){ $arr </span>= array(<span>"</span><span>name</span><span>"</span>,<span>"</span><span>age</span><span>"</span>);<span>//</span><span>数组中的成员$name和$age将被串行化,成员$sex则将被忽略。</span> <span>return</span><span>($arr);<span>//使用__sleep()方法的时候必须返回一个数组。</span> } <span> //在反串行化对象时自动调用该方法,没有参数也没有返回值</span> function __wakeup(){ $</span><span>this</span>->age = <span>40</span><span>;<span>//在重新组织对象的时候,为新对象中的$age属性重新赋值</span> } } $person1 </span>= <span>new</span> Person(<span>"</span><span>张三</span><span>"</span>,<span>20</span>,<span>"</span><span>男</span><span>"</span><span>); $person1_string </span>=<span> serialize($person1); echo $person1_string.</span><span>"</span><span><br /></span><span>"</span><span>; </span><span>//</span><span>反串行化对象,并自动调用了__wakeup()方法重新为独享中的age赋值。</span> $person2 =<span> unserialize($person1_string); $person2</span>-><span>say(); </span>?>
The output result is:
O:<span>6</span>:<span>"</span><span>Person</span><span>"</span>:<span>2</span>:{s:<span>12</span>:<span>"</span><span>Personname</span><span>"</span>;s:<span>6</span>:<span>"</span><span>张三</span><span>"</span>;s:<span>11</span>:<span>"</span><span>Personage</span><span>"</span>;i:<span>20</span><span>;} 我的名字:张三性别为: 年龄为:</span><span>40</span>
2. Save the serialized string to the file, read the string from the file, and deserialize the instance.
header(<span>"</span><span>Content-type: text/html; charset=UTF-8</span><span>"</span><span>); </span><span>class</span><span> Person{ </span><span>private</span><span> $name; </span><span>private</span><span> $sex; </span><span>private</span><span> $age; function __construct($name,$age,$sex){ $</span><span>this</span>->name =<span> $name; $</span><span>this</span>->age =<span> $age; $</span><span>this</span>->sex =<span> $sex; } function say(){ echo </span><span>"</span><span>我的名字:</span><span>"</span>.$<span>this</span>->name.<span>"</span><span>性别为: </span><span>"</span>.$<span>this</span>->sex.<span>"</span><span>年龄为:</span><span>"</span>.$<span>this</span>-><span>age; } } $person1 </span>= <span>new</span> Person(<span>"</span><span>张三</span><span>"</span>,<span>21</span>,<span>"</span><span>男</span><span>"</span><span>); $person1_string </span>=<span> serialize($person1); file_put_contents(</span><span>"</span><span>file.txt</span><span>"</span>, $person1_string);<br /><br />
header(<span>"</span><span>Content-type: text/html; charset=UTF-8</span><span>"</span><span>); </span><span>class</span><span> Person{ </span><span>private</span><span> $name; </span><span>private</span><span> $sex; </span><span>private</span><span> $age; function __construct($name,$age,$sex){ $</span><span>this</span>->name =<span> $name; $</span><span>this</span>->age =<span> $age; $</span><span>this</span>->sex =<span> $sex; } function say(){ echo </span><span>"</span><span>我的名字:</span><span>"</span>.$<span>this</span>->name.<span>"</span><span> 性别为:</span><span>"</span>.$<span>this</span>->sex.<span>"</span><span> 年龄为:</span><span>"</span>.$<span>this</span>-><span>age; } } $person2_string </span>= file_get_contents(<span>"</span><span>file.txt</span><span>"</span><span>); $person2 </span>=<span> unserialize($person2_string);<span>//反串性化重新形成对象$person2.</span> $person2</span>-><span>say(); </span>?>