Rumah >pembangunan bahagian belakang >tutorial php >php中对象的串行化,php对象串行化_PHP教程
我们大家有知道PHP串行化可以把变量包括对象,转化成连续bytes数据,你可以将串行化后的变量存在一个文件里或在网络上传输,然后再反串行化还原为原来的数据。文章这里就PHP串行化为大家详细的介绍。你在反串行化类的对象之前定义的类,PHP可以成功地存储其对象的属性和方法. 有时你可能需要一个对象在反串行化后立即执行。为了这样的目的,PHP会自动寻找__sleep和__wakeup方法。
当一个对象被PHP串行化,PHP会调用__sleep方法(如果存在的话). 在反串行化一个对象后,PHP 会调用__wakeup方法. 这两个方法都不接受参数. __sleep方法必须返回一个数组,包含需要串行化的属性. PHP会抛弃其它属性的值。如果没有__sleep方法,PHP将保存所有属性。
<?<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>?>
输出的结果为:
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.将串行化的字符串保存到文件中,从文件中读取字符串,反串性化实例。
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>?>