tags:

views:

202

answers:

5

I've been trying to learn the object oriented side of PHP, and was wondering:

If I used a _constructor to open a connection to a database, used a function within that class (eg. insert), would the defined __destructor close the connection after the method "insert" is executed?

class data(){
  function __constructor {
    // connect to db
  }

  function insert($data){
    // mysql_query(...)
  }

  function __destructor {
    // close connection to db
  }
}

$obj = new db();
$obj->insert('mumbo jumbo');

Or would the connection to the database still be open? Cause I read that the destructor is only run if the object is destroyed. But how do you destroy an object?

+5  A: 

In PHP, an object is destroyed when it goes out of scope. This is normally when the script stops executing or when the function it was created within ends, but you can destroy an object early in your code using:

unset($my_variable);

So, to answer your question, you should be fine allowing the destructor to handle closing the DB for you in most situations, especially with small scripts.

Mark Hammonds
Note that that line will only result in the object being deallocated and its destructor being called if `$my_variable` is the last thing pointing to the object.
chaos
+2  A: 

Yes, that will work fine, as long as you use the correct names, __construct() and __destruct(), for your constructors and destructors, as opposed to what you have there.

chaos
A: 

The object is destroyed once there is no more reference to it, for example by unset()-ting the last variable holding the object or when the script execution terminates.

By the way, the magic methods are named __construct and __destruct, without the trailing -or.

Ferdinand Beyer
+1  A: 

BTW, the constructors and destructors are called __construct and __destruct.

__destructor would be called when there are no more references to the the db. Typically, this occurs when the object goes out of scope, but if you have saved other references to it, this won't happen. You can remove references to the db using

unset($obj);

and likewise if you have stored $obj anywhere.

Paul Biggar
A: 

Keep in mind that PHP also supports persistent connections to databases, which means that even if your object has been destroyed, the connection to DB is still open "in the background" and will be reused when you call the correspondending pconnect (or PDO analogue) the next time.

Anti Veeranna
To clarify, PHP supports persistent connections if you use mysql_pconnect() which is, generally-speaking, frowned upon. All the other mysql connect methods, AFAIK, automatically close when the script has finished executing.
Mike B
Oh there are others besides mysql_pconnect, like pg_pconnect, sybase_pconnect, etcThe are better ways in the world to do connection pooling, you are absolutely right about that. I just thought that this is a appropriate place to mention it, since the question was about 'closing database connection'.
Anti Veeranna