PHP 객체 참조를 제거하기 전에 직렬화하기 전에 복원하기 전에 복원하십시오

StackOverflow https://stackoverflow.com/questions/2233008

문제

디스크에서 캐시하고 싶은 객체가 있습니다. 이 과정에서 Serialize ()를 사용합니다. 객체에는 다른 객체에 대한 참조가 포함되어 있습니다. 나는 그것들이 시리얼링되기를 원하지 않습니다 (다른 곳에서는) 비조리화 할 때 동일한 실제 객체의 중복 인스턴스를 제공하기 때문입니다.

객체 참조를 문자열에 대한 객체 참조 (동일한 객체를 참조하지만 ID에 의해)를 변경하고 다시 변경하고 변경하고 클래스 코드 내부 에서이 작업을 수행하는 방법이 있습니까? ?

좋은:

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 ()를 찾고 있다고 생각합니다.

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

다른 팁

자아 참고 : 먼저 문서를 확인하십시오

http://www.php.net/manual/en/language.oop5.magic.php#language.oop5.magic.sleep

예. 살펴보십시오 __ 수면 및 __wakeup

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top