php 序列化与反序列化
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?php 2 //声明一个类 3 class dog { 4 5 var $name; 6 var $age; 7 var $owner; 8 9 function dog($in_name="unnamed",$in_age="0",$in_owner="unknown") { 10 $this->name = $in_name; 11 $this->age = $in_age; 12 $this->owner = $in_owner; 13 } 14 15 function getage() { 16 return ($this->age * 365); 17 } 18 19 function getowner() { 20 return ($this->owner); 21 } 22 23 function getname() { 24 return ($this->name); 25 } 26 } 27 //实例化这个类 28 $ourfirstdog = new dog("Rover",12,"Lisa and Graham"); 29 //用serialize函数将这个实例转化为一个序列化的字符串 30 $dogdisc = serialize($ourfirstdog); 31 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";} 32 33 ?>
工欲善其事,必先利其器。