Class deconstructors?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
imamember
Forum Newbie
Posts: 7
Joined: Thu Apr 15, 2004 7:00 am
Location: South Africa

Class deconstructors?

Post 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
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post 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.
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post 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.
imamember
Forum Newbie
Posts: 7
Joined: Thu Apr 15, 2004 7:00 am
Location: South Africa

Post 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?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

For PHP4 you might find the magic functions __sleep and __wakeup useful.
http://php.net/manual/en/language.oop.m ... ctions.php
imamember
Forum Newbie
Posts: 7
Joined: Thu Apr 15, 2004 7:00 am
Location: South Africa

Post 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?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Yeah, the connection is closed automatically when the script ends anyway :o
imamember
Forum Newbie
Posts: 7
Joined: Thu Apr 15, 2004 7:00 am
Location: South Africa

Post 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.
User avatar
PrObLeM
Forum Contributor
Posts: 418
Joined: Sun Mar 07, 2004 2:30 pm
Location: Mesa, AZ
Contact:

Post by PrObLeM »

Maybe this will help

Code: Select all

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

Code: Select all

$db->close();
Post Reply