views:

92

answers:

2

I'm quite new on php and am trying to learn it.

My question is rather simple but I've been a bit lost while googling for it.

I just want to create an object to manage the database connection. I already did the object and now the problem I'm facing is:

How do i keep it instanced to a session? (So that i don't need to open/close the connection to the database on every page load) And how do I call it afterward?

Are there any way to declare a destroyer, so that when the instance is dying, the connection to the database gets closed?

+3  A: 

If you define a __destruct() method on your object, it'll get called when the object is about to be destroyed. You can use this to close your database connection.

You can also use __sleep() and __wakeup() for serializing your objects. They'll get called automatically upon serialization and unserialization. You could use these methods to connect and disconnect as required.

JamShady
+1  A: 

I hate to answer by refuting the question but I think you are going about resolving the problem in the wrong way here.

Rather than worrying about the connection being opened/closed I would suggest using caching, indexing, etc. to solve performance problems when they arise rather than being concerned about the resources involved in establishing a connection.

If you are really concerned about performance why not cache the affected pages and avoid using the database connection at all?

I think you could get the desired effect with this function (don't use this myself, assuming mysql) but be sure to read the comments:

http://www.php.net/mysql-pconnect

I don't think you want to start using sleep/wakeup techniques as to get this working as I understand it would involve creating a whole bunch of separate threads each with its own database connection which will just sap your resources and produce the opposite of the intended effect.

Ben
The question isn't about performance, it's about how to maintain a db connection open. Caching/indexing/etc is a separate issue altogether, and even then, if you're using the db, you'll still need to address how to maintain the connection which is what this question is about.
JamShady
@JamShady: The question *is* about performance. Read the comments, the OP states "I just want to avoid being re-opening the connection for every page".
Tomalak
I did, but I read it as though the OP wants to avoid $db->connect() calls on each page once a connection has been established, and if this could somehow be automated, it's one less thing to remember how to do. That's nothing to do with performance, it's to do with maintaining state via the session.
JamShady
I think this is a bit of an misunderstanding about the way PHP works. Since the whole process is ran/terminated on each request there is no way you can avoid doing a connect of some kind unless you run it as a process run in memory. I think pconnect is the closest you will get and stay sane.
Ben
Or... if you use one of the magic functions, you can get PHP to connect to the db automatically for you, which is the substance of the question.
JamShady