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
A
回答
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
只是一個簡單的拼寫錯誤
相關問題
- 1. 什麼是[Serializable接口]和[序列化()]
- 2. 如何實現Serializable接口,序列化/反序列化類/從機類型
- 3. 獲取序列化的錯誤,即使插入[Serializable接口]
- 4. JAXB - Serializable接口
- 5. 序列化接口
- 6. 與接口序列化子集合
- 7. 查找不在項目[Serializable接口]
- 8. DTO實現Serializable接口
- 9. 的getString實現Serializable接口
- 10. 序列化Serializable類中的RSAKeyValue屬性
- 11. Jackson的序列化接口
- 12. 序列化嵌套接口
- 13. WCF序列化和接口
- 14. ObjectForScripting與多個接口不工作
- 15. JMS反序列化與XML列表,的XPath的SimpleXMLElement不工作
- 16. 序列化接口列表GSON
- 17. INotifyPropertyChanged的不工作的XML序列化
- 18. JSON序列化不Django的工作
- 19. 序列化ArrayList android不工作
- 20. 反序列化在WCF中不工作
- 21. 將序列化爲意圖不工作
- 22. laravel AJAX序列化後不工作
- 23. 使用ServiceStack.Text反序列化CSV與CustomHeaders:不工作?
- 24. 獲取列表[Serializable接口]而不是列表[(字符串,字符串)
- 25. 如何阻止hibernate工具實現Serializable接口
- 26. 排序()不與列表解析工作
- 27. 接口和JSON序列化在.net
- 28. 接口和對象反序列化
- 29. 使用com.fasterxml.jackson.databind.ObjectMapper序列化接口字段
- 30. 將接口對象序列化爲JSON
對象序列化嗎?或者,也許問題是在非序列化? – Voitcus 2013-03-12 20:25:12
@volitcus它不會遇到任何錯誤。和'var_dump(unserialize(serialize($ this-> object)))''返回的對象,但只有默認的屬性值(寫在OP中的註釋) – varuog 2013-03-12 20:29:28
但var_dump(serialize($ this-> object) );'返回? – Voitcus 2013-03-12 20:31:50