php对象序列化接口(Serializable)

2021-10-16
  1. 实现此接口的类将不再支持__sleep()和__wakeup()
  2. 当实例被序列化时,类将调用serialize方法而不是调用__destruct()
  3. 当实例被反序列化时,类将调用unserialize()方法而不是调用__construct()
class Example implements Serializable
{
    protected $property1;
    protected $property2;
    protected $property3;

    public function __construct($property1, $property2, $property3)
    {
        $this->property1 = $property1;
        $this->property2 = $property2;
        $this->property3 = $property3;
    }

    public function serialize()
    {
        return serialize([
            $this->property1,
            $this->property2,
            $this->property3,
        ]);
    }

    public function unserialize($data)
    {
        list(
            $this->property1,
            $this->property2,
            $this->property3
        ) = unserialize($data);
    }

}));

 

{/if}