# PHP: When is __destruct() called?



## Jenski (Jul 15, 2008)

Hi, 
I'm I've been programming in PHP4 for a few years now and am slowly moving over to PHP5, I was wondering when developing a class when the __destruct() function is called? I have put all my closing connections in it, and tried to print to screen for confirmation but it doesn't print anything. 

Any thoughts?

Thanks in advance


----------



## jamiemac2005 (Jul 5, 2007)

hmm, could you post some code? sorry i haven't used PHP in a while, but as a general programming concept you only destroy an instance of a class when you're done with it (e.g. once everything's been output)... but i don't know the situation so if you were to post code it would help.

Cheers,
Jamey


----------



## Jenski (Jul 15, 2008)

```
<?php 
class AD{

	private $host 			= "ldap://xx";
	private $port 			= "";								//PHP: $port
	private $default_user	= "xx";
       //PHP: $bind_rdn
	private $default_password = "xx";					         //PHP: $bind_password
	private $base_dn		= "DC=xx,DC=xx,DC=xx";

	private $ous			= array('xx','xx','Techs','xx','Groups'); //OUs (who's parent is root in AD )in use for the Intranet 
	
	//The Intranet should only mess with these attributes of each object, none others
	private $attributes		= array('member');
	
	private $connection;	//PHP: $link_identifier
	private $bind_conn;
	private $debug;			//Array of Debugging messages
	

	/*
		Constructor- creates a new instance of the AD class
		Opens Connection to LDAP
		Binds a default user used for Searching etc
	*/
	public function __construct() {
		$this->debug[]	= "Constructor Called";
		$this->open_connection();
		$this->bind_default_user();
	}
	
	/*
		Destructor
		Makes sure connection is closed if called
	*/
	public function __destruct(){
		$this->debug[]	= "Destructor Called";
		$this->close_connection();
	}
	
	/*
		Open LDAP Connection 
	*/
	private function open_connection(){
		if(empty($this->port)){
			$this->connection = ldap_connect($this->host) or die("Could not connect to LDAP Host");
			$this->debug[]	= "LDAP Connect successful";
		}else{
			$this->connection = ldap_connect($this->host, $this->port) or die("Could not connect to LDAP Host");
			$this->debug[]	= "LDAP Connect successful";
		}
	}
	
	/* 
		Creates Default Active Directory Bind
	*/
	private function bind_default_user(){
		 $this->bind_conn = ldap_bind($this->connection, $this->default_user, $this->default_password);
		 if ($this->bind_conn) {
			$this->debug[]	= "LDAP Default User bind successful";	
    	} else {
        	$this->debug[] = "ERROR: LDAP Default User bind unsuccessful";
    	}
	}
	
	/* 
		Closes Active Directory Connection
	*/
	private function close_connection(){
		ldap_close($this->connection);
		$this->debug[]	= "LDAP Close connection called";	
	}
	/*
		Debugging Function
		Displays Errors/Messages throughout runtime of executing code
	*/
	public function debugging(){
		for($i=0;$i<sizeof($this->debug);$i++){
			echo $this->debug[$i] . '<br/>';
		}
	}
}
?>
```


----------



## Jenski (Jul 15, 2008)

Sounds as though it should be done automatically, but for some reason it doesn't?

http://uk.php.net/manual/en/language.oop5.decon.php


----------

