473,385 Members | 1,521 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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(parent::__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.serialize]: "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
Jul 17 '05 #1
0 1463

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: John Smith | last post by:
I have a user assigned multiple roles and a role can be inherited from multiple parents (see below). How do I answer such questions as "How many roles does the user belongs to?" I answered the...
1
by: Hannes Heckner | last post by:
I have: <data> <item> <object>bla</object> </item> <item> </item> <item> <object>blabla</object>
3
by: PYCTAM | last post by:
Hi, I have a table with filled out below data: +------+-----+ |parent|child| +------+-----+ |A |B | |B |C | |B |E |
6
by: Luke Dalessandro | last post by:
I'm not sure if this is the correct forum for platform specific (Mozilla/Firefox) javascript problems, so just shout and point me to the correct newsgroup if I'm being bad. Here's the deal... ...
19
by: Swaregirl | last post by:
Hello, I would like to build a website using ASP.NET. I would like website visitors to be able to download code that I would like to make available to them and that would be residing on my...
18
by: yinglcs | last post by:
Hi, I have a newbie XSLT question. I have the following xml, and I would like to find out the children of feature element in each 'features' element. i.e. for each <featuresI would like to...
1
by: esimons | last post by:
I am searching for days now to get a concrete example of how to use the __sleep() and __wakeup() function in a class to restore a databaseconnection when passing an object from one page to another by...
3
by: krzysztof.konopko | last post by:
Hello! I want to design a class hierarchy with one base abstract class, let's say CBase. I have a predicate that every object in the class hierarchy must have a parent object of type from this...
2
by: McSwell | last post by:
I have an XML file that looks something like this: ------------------------ <?xml ...?> <!DOCTYPE ...> <src:fragment id="foo" ...> <Ph:Itemsome stuff </Ph:Item> <Ph:Itemsome more stuff...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.