2010-02-09 1 views
1

디스크에 캐시하려는 일부 개체가 있습니다. 나는이 과정에서 serialize()를 사용한다. 객체에는 다른 객체에 대한 참조가 포함되어 있습니다. 그것들을 직렬화 할 때 같은 실제 세계 객체의 중복 인스턴스를 줄 것이기 때문에 그것들을 직렬화하는 것을 원하지 않습니다.PHP는 직렬화하기 전에 개체 참조를 제거하고, 다음을 복원합니다.

객체 참조를 직렬화하고 다시 변경하기 전에 객체 참조를 문자열로 변경하는 방법 (동일한 객체를 참조하지만 ID로 변경)은 클래스 코드 내부에서 수행해야합니다 (예 : un) serialize statement)?

좋은 :

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

답변