Question about PHP class and mysql connector

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
WilliamLou
Forum Newbie
Posts: 12
Joined: Fri Mar 14, 2008 1:32 pm

Question about PHP class and mysql connector

Post by WilliamLou »

Hi everyone:
I have a question about PHP class, for example: I want to create a class in PHP for a mysql connector.
The class definition is like:
Class Database
{
var $dbh = null;
function Database ($dbc) //constructor
{
$this->dbh = mysql_connect($dbc['db_host'], $dbc['db_user'], $dbc['db_pass'], TRUE);
}
.....and several member functions to operate on those DB tables
}

Now, because I need to run the program as a backend daemon, the way I achieve this is through a infinite loop:
while (true)
{
$db_delegation = new Database ($db_settings);
.....processing on $db_delegation
sleep($time);
}

This works fine when I test the program, however I noticed some problems:

1. The mysql connector resource id (class instance $db_delegation->dbh) keeps increasing. If I run the backend daemon for a whole day, the resource id increased to 15399 and it still works fine (because in the php configure file, the #limit of mysql connector is unlimited). I'm not sure if php could free the mysql connector automatically. According to the manual, php will free the resource whenever the script ends, however my script is a backend daemon so that it will never ends.

2. Actually, I could close mysql connector explicitly in my php script (just call mysql_close ($db_delegation->dbh) as the last processing in while loop, right?) However, it's not like OO style. In that case, I want to put the mysql_close in the class's destructor and then destroy that class at the end of while loop. Then, the question is how to destroy the class in php? Just call unset ($db_delegation) and then php will call its destructor automatically, right?


Anyone could help to clarify this problem? Thanks a lot!!!
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Re: Question about PHP class and mysql connector

Post by Mordred »

You need only one such connection. Move the new statement before the loop. In the loop call another method of the Database class which would call mysql_ping( ) to keep the connection alive, or reconnect if it's needed.

There are no real destructors in PHP. There are ways to simulate them, but with a mysql connection you don't need to - when the script terminates so will the connection.
WilliamLou
Forum Newbie
Posts: 12
Joined: Fri Mar 14, 2008 1:32 pm

Re: Question about PHP class and mysql connector

Post by WilliamLou »

Thanks a lot for your reply, Mordred. At least, I know the function mysql_ping now. From the comments in the php manual, it seems like some guys don't trust mysql_ping's reconnect feature. Actually, I used mysql_connect with newlink flag to be TRUE, because if the database server is down in the middle, it could reconnect and establish a valid resource id. Otherwise, without the newlink flag, I think mysql_connect will default use the same connection.

In short, I think mysql_ping can't solve the leak problem (if it reconnect, it will create a new mysql connector but won't free/close the previous one). And PHP won't free those resources (sql connections etc.), because the script never ends. Am I right?

Of course, mysql_ping won't let the resource be exhausted so quickly as the method I did. Thanks, Mordred.
Mordred wrote:You need only one such connection. Move the new statement before the loop. In the loop call another method of the Database class which would call mysql_ping( ) to keep the connection alive, or reconnect if it's needed.

There are no real destructors in PHP. There are ways to simulate them, but with a mysql connection you don't need to - when the script terminates so will the connection.
Post Reply