Is the use of __destruct redundant in this scenario?

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
Stacks
Forum Newbie
Posts: 24
Joined: Thu Jun 05, 2008 7:52 pm

Is the use of __destruct redundant in this scenario?

Post by Stacks »

I'll just use this stripped down version of my login data access layer class as an example. Now after instantiating the DB class i figured i'd destroy it when the Login_DAL class is destroyed. However when removing the __destruct the PHP garbage collector seems to destroy the DB object itself. So since PHP does this on it's own is it redundant to unset my objects? Or would it be wise to leave the destructor methods in and unset all references to instantiated objects manually for better programming practices? I'd like to hear what you guys do in your own applications.

Code: Select all

class Login_DAL
{
	private $db = null;
	function __construct()
	{
		$this->db = new DB();
	}
	public function addLogin($email, $password)
	{
		mysql_query("insert into login(email, Password) values('$email', '$password');");
		return mysql_insert_id();
	}
	function __destruct()
	{
		unset($this->db);
	}
}
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Is the use of __destruct redundant in this scenario?

Post by requinix »

PHP does a good job at cleaning up after you. I generally don't use destructors, but they would be useful when you need to do non-object-destruction stuff - for example, logging stuff and unlocking files.
Post Reply