2013-03-12 79 views
0
class foo implements serializable 
    { 
    public function serialize() { 

      return serialize($this); 
     } 

     public function unserialize($serialized) 
     { 
      //Revive connection 
      AbstractContent::$connection= DatabaseHandle::getConnection(); 
      return unserialize($serialized); 
     } 
    }  


var_dump($this->object); //$this object is instance of class foo returns the exact dump of objects with correc property 

    var_dump(unserialize(serialize($this->object))); //Does not dump correctly. only default property values are evaluated, constructor assigned values are ignored, computed values are ignored 
+0

對象序列化嗎?或者,也許問題是在非序列化? – Voitcus 2013-03-12 20:25:12

+0

@volitcus它不會遇到任何錯誤。和'var_dump(unserialize(serialize($ this-> object)))''返回的對象,但只有默認的屬性值(寫在OP中的註釋) – varuog 2013-03-12 20:29:28

+0

但var_dump(serialize($ this-> object) );'返回? – Voitcus 2013-03-12 20:31:50

回答

2

由於每docs

When the data is unserialized the class is known and the appropriate unserialize() method is called as a constructor instead of calling __construct(). If you need to execute the standard constructor you may do so in the method

而且,你沒有做任何邏輯當使用serialize()時,您可以輕鬆覆蓋_seelp() and __wakeup() magic methods而不是實施serializable。例如:

class foo 
{ 
    public function __wakeup() { 
     // Do your logic here 
    } 
} 
$foo = new foo(); 
var_dump(unserialize(serialize($foo))); 
1
class foo implements serilizable 

應該是:

class foo implements serializable 

只是一個簡單的拼寫錯誤

+0

抱歉,這是我的錯誤。實際的代碼是可以的。更新了問題。問題依然存在。 – varuog 2013-03-12 20:28:04

+0

我會把它帶出一個類,只是使用內置的序列化函數 – 2013-03-12 20:30:56

+0

沒有可序列化的接口,如果我直接序列化,我馬上得到這個錯誤'致命的錯誤:找不到執行方法bar :: serialize在未知line 0' bar class的一個對象是foo類的屬性。不知何故,它不是序列化嵌套的對象。 – varuog 2013-03-12 20:39:48