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!
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.
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);
}
}
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.