Page 1 of 1

Class deconstructors?

Posted: Thu Apr 15, 2004 7:00 am
by imamember
Hi there

I have a bit of a problem...or question, please help if you can...

Basically lets say you create a class that in its constructor creates a DB connection. Now theoretically in other languages you would have to close that connection at some point to prevent hassles. It strikes me that the best place to do this would be in the deconstructor...BUT of course PHP doenst have a deconstructor.

How / What is the best way to do this in PHP? I do need to close that connection correct? Where would you suggest I do that?

AND does PHP have overloading of functions?

Thanks a mill. :P

Posted: Thu Apr 15, 2004 7:05 am
by magicrobotmonkey
I don't think there is overloading of functions. I know you can't overload constructors like in java, so I asume that holds true for all functions. And as for you DB disconnect, just have a disconnect() function that you call when its time to disconnect. I've never really understood deconstructors anyways, having learned OOP in java where the garbage collection is all automatic. I think it has made me sloppy.

Posted: Thu Apr 15, 2004 7:13 am
by redmonkey
I believe deconstuctors are available in PHP5 (not sure as I haven't looked to closely at it yet).

As a bit of a workaround in PHP4 you could try register_shutdown_function, which may suit your needs.

Posted: Thu Apr 15, 2004 9:33 am
by imamember
The only thing with this is that, it seems to run through it all straight away? Not giving me time to pass the calsee around and use the connection? ie. before I can use it it's closed?

Posted: Thu Apr 15, 2004 9:37 am
by markl999
For PHP4 you might find the magic functions __sleep and __wakeup useful.
http://php.net/manual/en/language.oop.m ... ctions.php

Posted: Thu Apr 15, 2004 9:47 am
by imamember
mmm heavy, do you know if you HAVE to close the DB connection to mysql? or does it have some kind of garbage disposeR?

Posted: Thu Apr 15, 2004 9:51 am
by markl999
Yeah, the connection is closed automatically when the script ends anyway :o

Posted: Thu Apr 15, 2004 9:53 am
by imamember
Shweet no worries then, fraid I'm going to take the easy way out for now :P SO bad!! Happy though, everything I ned is working, shot for the help.

Posted: Thu Apr 15, 2004 10:41 am
by PrObLeM
Maybe this will help

Code: Select all

/*
	Close Mysql connection Function
*/		
	function close() {
		mysql_close();
	}

Code: Select all

$db->close();