Guest User

bug #1/working

a guest
Feb 18th, 2015
20
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. class testClass_forSerialize implements Serializable{
  3.     public function serialize()
  4.     {
  5.         // use array instead of stdClass
  6.         $ret = Array();
  7.         foreach($this as $k=>$v){
  8.             if ($v!==null) {
  9.                 $ret[$k] = $v;
  10.             }
  11.         }
  12.  
  13.         $serializedData = serialize($ret);
  14.  
  15.         if (unserialize($serializedData)===false) {
  16.             var_dump($serializedData);
  17.             throw new \Exception("Serialize failed");
  18.         }
  19.  
  20.         return $serializedData;
  21.     }
  22.  
  23.     public function unserialize($data)
  24.     {
  25.         $dataObj = unserialize($data);
  26.         foreach($dataObj as $k=>$v) {
  27.             $this->{$k} = $v;
  28.         }
  29.     }
  30. }
  31.  
  32.  
  33. class testClass_forSerialize_02 extends \testClass_forSerialize{
  34.     public $list = Array();
  35.  
  36.     public function __construct()
  37.     {
  38.         for ($i=0; $i<10; $i++) {
  39.             $this->list[] = new \testClass_forSerialize();
  40.         }
  41.     }
  42. }
  43.  
  44.  
  45. $obj = new \testClass_forSerialize_02();
  46.  
  47. $ser = serialize($obj);
  48. $obj2 = unserialize($ser);
RAW Paste Data