-1

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

  1. 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.

  1. 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 ...

}
  • Could you not just write your own serialize function for card, then write a serialize function for packs (which calls the serialize on each card) etc. until you have written the final serialise box method? Also the point of turning an object into a string is to make it readable to the human eye. Why do you need to deserialize the string to an object? In anycase, you might find this informative. http://community.sitepoint.com/t/object-equivalence-after-serialize/90031/5 – Satbir Kira Aug 25 '15 at 14:26
  • The plan is to persist objects in the session. I either serialize the objects or store the data and rebuild the objects which seems like reinventing the wheel. – Matthew Brown aka Lord Matt Aug 25 '15 at 14:39
  • This may help. http://stackoverflow.com/questions/1442177/storing-objects-in-php-session. I've just looked over it a bit, it says "objects are automatically serialized to $_SESSION, so explicit serialization isn't necessary" – Satbir Kira Aug 25 '15 at 14:47
  • The question is about making sure the object can be serialized. – Matthew Brown aka Lord Matt Aug 25 '15 at 22:43

1 Answers1

-1

From looking at the docs, $_SESSION will automatically serialize and store objects for you.

Example: http://example.preinheimer.com/sessobj.php

Satbir Kira
  • 792
  • 6
  • 21
  • Assuming the objects can be... However these objects contain pointers to other objects. – Matthew Brown aka Lord Matt Aug 25 '15 at 14:58
  • Have you tried to store it in in session? I don't see why php wouldn't take care of the low level pointer details for you. – Satbir Kira Aug 25 '15 at 15:04
  • The problem was expected (and now proven to be) the child objects and pointers thereof. PHP tries and fails to make them into ints. See update #1. – Matthew Brown aka Lord Matt Aug 25 '15 at 15:08
  • Then it is safe to say that objects you put into session will not be serialized, and deserialized correctly. There is nothing more you can do since http is stateless. You will not be able to get all states correctly like they were, and that is a guarantee. So you will have to serialize it by hand yourself an do what you wish (put it in session, or serialize to json and store in a cookie). – Satbir Kira Aug 25 '15 at 15:15