473,656 Members | 2,921 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Jul 17 '05 #1
0 1478

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

Similar topics

1
2544
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 above questions by using .NET but I think it can be more efficient by using just SQL. I would appreciate if you can give me an answer. Thank you.
1
4147
by: Hannes Heckner | last post by:
I have: <data> <item> <object>bla</object> </item> <item> </item> <item> <object>blabla</object>
3
3532
by: PYCTAM | last post by:
Hi, I have a table with filled out below data: +------+-----+ |parent|child| +------+-----+ |A |B | |B |C | |B |E |
6
13350
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... html file (generated using .NET 2.0 beta2): <form method="post" action="Test2.aspx" id="form1">
19
2264
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 personal server. Are there any code samples or books that someone can recommend so that I can implement this. I would prefer VB.NET code, but I am willing to convert from C# if necessary.
18
2066
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 look up what each feature depends on and gerenate a text file. For example, in the following file, I would like to find out feature A depends on A1 and A2 and feature B depends on B1 and B2. And write that to a text file.
1
1939
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 putting it in a session-variable. What I already know is that you should load the class before the session_start() function etc... What I DO NOT know is how the concrete code in the __sleep() and __wakeup() functions should be in order to...
3
3325
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 class hierarchy but only objects of some of these types can be a parent. Example: class CFruit : public CBase { }; class CBranch : public CBase { };
2
2765
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 </Ph:Item> .... <Ph:Itemstill more stuff </Ph:Item> </src:fragment>
0
8816
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8717
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8498
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8600
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7311
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5629
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.