2

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?

2
  • I think you mean $obj = new data(), not db() since you called your class data. You mention persistent connections --What mysql connection method are you using? I think mysql_pconnect() is the only way to instantiate a persistent connection with mysql. Commented Aug 19, 2009 at 18:36
  • don't take my code above too seriously, I was merely trying to get my point across. My main question above was to achieve a secure connection by opening and closing the connection to the DB when necessary, as some how increase the security & performance of the website... Commented Aug 19, 2009 at 21:41

5 Answers 5

6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
3

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.

Comments

2

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.

Comments

1

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.

Comments

0

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.

2 Comments

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.
Oh there are others besides mysql_pconnect, like pg_pconnect, sybase_pconnect, etc The 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'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.