For a PHP project I working on I have an object (a singleton actually but that's not hugely important, I am aware that I will need to make a special case for the static value(s)). This object contains protected arrays each with pointers to between ten and thirty other objects.
What is the simplest way to serialize (and also unserialize) the entire data structure while maintaining all the relevant references and indexes (which is what most of the arrays are)?
details
The program deals with cards which are each represented by objects and these objects are collected by packs (also objects) and registered to an object called box. Packs also register to box (passing by reference the object) which maintains an index of both. Box carries out various operations (like getting a random card from each pack and adding it to another array (as pointer) and creating a mirror array (less these cards) called index which it shuffles. The cards are then "dealt" between instances of the object player (which will also probably register with box).
It would be nice to think that I could simply serialise box and everything would be fine but I strongly doubt this. How can I be sure that all the references (arrays of pointers) are intact and correct (and not copies of the objects) after the mess of objects has become a string and gone back to being objects again?
UPDATES
- I attempted to simply dump box with serialize and got
Object of class pack could not be converted to int in /path/to/box.class.php on line XYZ
To be honest that is what I expected. Thus my question about how I go about doing this.
- Maybe I am communicating poorly? Perhaps some code will make this clearer.
Note just how much by reference storage we have going on. How do I implement the Serializable interface to account for this?
<?php
class box{
public $cards = array();
public $index = array();
protected $solution = array();
public $packs = array();
public function shuffle(){
if(count($this->index==0)){
$this->index = $this->cards;
}
shuffle($this->index);
}
public function set_up(){
$this->index = $this->cards;
foreach($this->packs as $pack){
$card=$pack->chooseAtRandom();
unset($this->index[$card->getID()]);
$this->solution[]&=$card;
}
$this->shuffle();
}
public function registerPackToBox(&$pack){
$this->packs[] &= $pack;
return (count($this->packs)-1);
}
public function registerCardToBox(&$card){
$this->cards[] &= $card;
return (count($this->cards)-1);
}
// ... other stuff ...
}