Php Destructor

What is Php Destructor?

destructors – a function to be called when an object is deleted. PHP 5 introduces a destructor concept similar to that of other object-oriented languages, such as C++.
PHP calls destructors as soon as objects are no longer available, and the destructor function, __destruct(), takes no parameters.
void __destruct ( void )

why use destructor?
It gives the object an opportunity to prepare to be killed. This could mean manual cleanup, state persistence, etc.

example
you have create database object to communicate with database server after communicate with database server then you want to free database object through __destruct () method.


<?php 
function __construct()
{
  $this->link = mysql_connect($this->server.':'.$this->port, $this->username);
  if(!$this->link)
    die('Could not connect: '.mysql_error());

  if(!mysql_select_db($this->database, $this->link))
    die('Could not select database: '.mysql_error());
}    

function __destruct()
{
  if(mysql_close($this->link))
    $this->link = null; 
}
?>