Children: how do you make your parents __sleep() ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rob Long

    #1

    Children: how do you make your parents __sleep() ?

    Hey

    I'm building a webapp with tons of objects all over the place, a lot
    of which are quite complex, meaning they could have many subclasses
    and many nested object members. I'm implementing a memory caching
    mechanism to store these objects between requests and share them
    between different user sessions.

    In order for my memory caching mechanism to work, objects must be
    serialised before they are placed into the cache. In PHP5, this
    presents some problems for me, mostly to do with having multiple
    copies of the same object floating around after an object is returned
    from the cache (unserialised). I'm getting around this by preparing my
    objects for caching using the __sleep() method: serialising only
    certain instance variables and then using __wakeup() to ensure I'm
    still only working with one copy of any object.

    My question is: how do you use __sleep() in conjunction with a parent
    class whose members are private?

    I'm having problems as I've declared most of the instance variables in
    the classes of my inheritance hierarchy to be private. This means that
    I cannot recursively call __sleep() on parent classes in order to
    merge the variables together and return a complete array of variable
    names from the top-level function.

    An example:

    class MyParent
    {
    private $var1; // serialise
    private $var2; // don't' serialise

    function __construct()
    {
    $this -> var1 = "Hello World";
    $this -> var2 = "Goodbye World";
    }

    function __sleep()
    {
    return array("var1");
    }
    }

    class MyChild extends MyParent
    {
    private $var3; // serialise
    private $var4; // don't serialise

    function __construct()
    {
    $this -> var3 = "Hello World II";
    $this -> var4 = "Goodbye World II";
    }

    function __sleep()
    {
    return array_merge(par ent::__sleep(), array("var3"));
    }
    }

    $mc = new MyChild();
    $mcSerialised = serialize($mc);

    When I try to serialise $mc I get the following notice due to $var1's
    access modifier in MyParent:

    Notice: serialize() [function.serial ize]: "var1" returned as member
    variable
    from __sleep() but does not exist in /var/www/...

    So now I'm thinking that if I just call parent::__sleep () in the
    __sleep() function of MyChild then PHP will figure out that I'm
    trying to serialise the instance members of my parent class too.
    Doesn't work. In fact I can't think of any way to do this, other than
    with some horrendous hacks, or just by declaring all my instance
    variables to be protected.

    Has anyone seen this before? Can anyone think of a workaround?
    Obviously it wouldn't have been a problem in PHP4, as there were no
    private/protected access modifiers. Perhaps this means that __sleep()
    and __wakeup() don't really work for inherited objects in PHP5?

    Thanks,
    Rob
Working...