PHP序列化之前删除对象的引用,后恢复
-
19-09-2019 - |
题
我有,我想缓存磁盘上的某些对象。我在这个过程中使用的序列化()。该对象包含对其他对象的参考。我不希望那些被序列化,以及(即在其他地方做),因为它会给反序列化时,我重复相同的现实世界对象的实例。
序列化和后背部改变它们之前是否有一种方法来改变为字符串的对象的引用(是指相同的对象,但是通过ID),而要做到这类代码内(未之前和(UN后)序列化语句)?
好:
class TheStuff {
private $otherThing;
private function __yeahDudeDoThisOnSerialize() {
$this->otherThing = $this->otherThing->name;
}
private function __viceVersa() {
$this->otherThing = get_thing_by_name($this->otherThing);
}
}
serialize($someStuff);
为:
class TheStuff {
private $otherThing;
public function yeahDudeDoThisOnSerialize() {
$this->otherThing = $this->otherThing->name;
}
public function viceVersa() {
$this->otherThing = get_thing_by_name($this->otherThing);
}
}
$someStuff->yeahDudeDoThisOnSerialize();
serialize($someStuff);
$someStuff->viceVersa();
解决方案
我认为你正在寻找__sleep()和__wakeup()。
其他提示
不隶属于 StackOverflow