Pergunta

Eu tenho alguns objetos que eu gostaria de cache no disco. Eu uso serialize () neste processo. Os objetos contêm algumas referências a outros objetos. Eu não quero aqueles a ser serializado bem (o que é feito em algum outro lugar), porque ele me daria duplicar instâncias do mesmo objeto do mundo real quando unserializing.

Existe uma maneira de mudar as referências de objeto para cordas (referindo-se os mesmos objetos, mas por ID) antes de serialização e alterá-los de volta depois, e para isso dentro do código de classe (não antes e depois da (un) declarações serialize)?

Bom:

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);

Bad:

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();
Foi útil?

Solução

Eu acho que você está procurando __sleep () e __wakeup ().

http://php.net/manual/en/language.oop5 .magic.php

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top