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
4

2 回答 2

2

根据文档

当数据被反序列化时,类是已知的,并且适当的 unserialize() 方法作为构造函数调用,而不是调用 __construct()。如果您需要执行标准构造函数,您可以在方法中执行此操作

此外,由于您在使用时没有做任何逻辑,serialize()您可以通过简单地覆盖_seelp() 和 __wakeup() 魔术方法而不是实现serializable. 就像是:

class foo
{
    public function __wakeup() {
        // Do your logic here
    }
}
$foo = new foo();
var_dump(unserialize(serialize($foo)));
于 2013-03-12T20:51:30.983 回答
1
class foo implements serilizable

应该:

class foo implements serializable

只是一个简单的拼写错误

于 2013-03-12T20:25:15.237 回答