|
Posted by Marcus Bointon on 10/13/13 11:24
I'm sorting out some code to handle session object storage,
particularly where the objects contain DB connections. I've found
lots of articles that go on about how __sleep should clean up db
stuff, close connections etc and how __wakeup should reconnect, but
weirdly enough I've not found a single concrete example of doing this!
It's also not quite clear how this behaviour interacts with
persistent connections. For example, If I do this:
class foo {
protected $db;
public function __construct() {
$this->db = mysql_pconnect(....);
}
protected function __wakeup() {
$this->db = mysql_pconnect(....);
}
protected function __sleep() {
mysql_close($this->db);
$this->db = NULL;
return array();
}
}
given that the connection is persistent, and may thus be used by
other scripts, is calling mysql_close a particularly bad idea? Should
I just not bother closing the connection, letting PHP deal with it -
the object will not attempt to re-use the stale connection because it
will get a new instance courtesy of __wakeup when unserialized.
I'm also unclear about how __sleep acts in subclasses - it seems that
as soon as I have a __sleep function in a base class, I don't have
any choice but to implement __sleep functions in every subclass of
it, even if I don't want them to do anything that would not be done
if __sleep were not defined. in the exampel, the base class has no
properties to save, so it returns an empty array from __sleep, but
that's unlikely to be useful for a subclass that does have properties
(and serializing an object without any properties is pointless!).
Ideas?
Marcus
--
Marcus Bointon
Synchromedia Limited: Putting you in the picture
marcus@synchromedia.co.uk | http://www.synchromedia.co.uk
Navigation:
[Reply to this message]
|