473,385 Members | 1,796 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.

_sleep and _wakeup

I didn't understand from this

http://in2.php.net/manual/en/languag...-functions.php

whether we, the programmer, have to implement _sleep and _wakeup (like
..NET's IDisposable.Dispose()) or it is done by PHP internally on a
class that uses resources?

Jun 17 '06 #1
4 4104
Water Cooler v2 wrote:
I didn't understand from this

http://in2.php.net/manual/en/languag...-functions.php

whether we, the programmer, have to implement _sleep and _wakeup (like
.NET's IDisposable.Dispose()) or it is done by PHP internally on a
class that uses resources?


Hi,

__sleep and __wakeup are the methods, that you can implement in your
class. They are just like events that occur before serialization and
after unserialization. Consider this:

class MyClass {
function __sleep() {
echo "__sleep\n";
return array();
}
function __wakeup() {
echo "__wakeup\n";
}
}

$c = new MyClass();
echo "Sleep: ";
$str = serialize($c);
echo "Wakeup: ";
$c1 = unserialize($str);

The output will be:
Sleep: __sleep
Wakeup: __wakeup

Sincerely,
Alexander
http://www.alexatnet.com/

Jun 17 '06 #2
Thanks. That answers it. The onus of cleaning up and restoring
"resources" (if the class uses any) is with us, and hence the
implementation of these methods is a way given to us to do that before
serialization and after deserialization.

Thanks, again. :-)

Jun 17 '06 #3

Water Cooler v2 wrote:
Thanks. That answers it. The onus of cleaning up and restoring
"resources" (if the class uses any) is with us, and hence the
implementation of these methods is a way given to us to do that before
serialization and after deserialization.

Thanks, again. :-)


You misunderstood I'm afraid. IDisposable.Dispose called, if I remember
my .NET correctly, when an object is garbage collected. __sleep() is
called when an object is serialized--usually at the end of the request
when PHP saves session data to a file. Its main purpose is to let PHP
know what properties within the object should be saved and what
shouldn't be (cached records for instance). The __wakeup() method is
the inverse of that--called when PHP restore data previously stored in
a file into an object.

__destruct() is the closest counter part to IDisposable.Dispose. You
typically don't need to manually free resources, since PHP will do it
for you at the end of a request.

Jun 18 '06 #4

Chung Leong wrote:
You misunderstood I'm afraid. IDisposable.Dispose called, if I remember
my .NET correctly, when an object is garbage collected. __sleep() is
called when an object is serialized--usually at the end of the request
when PHP saves session data to a file. Its main purpose is to let PHP
know what properties within the object should be saved and what
shouldn't be (cached records for instance). The __wakeup() method is
the inverse of that--called when PHP restore data previously stored in
a file into an object.

__destruct() is the closest counter part to IDisposable.Dispose. You
typically don't need to manually free resources, since PHP will do it
for you at the end of a request.

Thanks, Chung. I'd understood correctly, albeit my anology was rather
amiss.

Jun 18 '06 #5

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

Similar topics

23
by: Antoon Pardon | last post by:
I have had a look at the signal module and the example and came to the conclusion that the example wont work if you try to do this in a thread. So is there a chance similar code will work in a...
9
by: phil | last post by:
And sorry I got ticked, frustrating week >And I could help more, being fairly experienced with >threading issues and race conditions and such, but >as I tried to indicate in the first place,...
21
by: Alo Sarv | last post by:
Hi From what I have understood from various posts in this newsgroup, writing event loops pretty much comes down to this: while (true) { handleEvents(); sleep(1); // or _sleep() or...
1
by: Anthony Knittel | last post by:
i've got an expensive loop that keeps running for a long time, whats the best way to put breaks in the program to return a bit of control to the user instead of locking up the system? its just a...
3
by: MarkN | last post by:
modf() is a C Standard Library function double modf(double x, double* ip); returns fractional part and assigns to *ip integral part of x, both with same sign as x I have encountere a situation...
0
by: skip | last post by:
I just noticed the announcement of Shed Skin 0.0.16 on Freshmeat with this (partial) change announcement: Changes: frozenset was added. time.sleep now works on Win32. Given Python's highly...
5
by: momobear | last post by:
I feel really puzzled about fellowing code, please help me finger out what problem here. import threading class workingthread(threading.Thread): def __init__(self): self.quitEvent =...
17
by: joebenjamin | last post by:
This is a problem I was trying to help a few friends figure out for fun. I am not sure how to go about this, but there is something I am missing here. Here is what we want the output to be: Need...
0
by: Tim Golden | last post by:
Lowell Alleman wrote: Well you've certainly picked a ticklish area to run into problems with ;). First, forget about the threading aspects for the moment. AFAICT the smallest program which...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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.