Website Templates Search

Destructor basics in php

  • Click to view live demo By Admin
  • Click to view live demo Jan 05, 2019

A “destructor” is a special member function in the class. It is used to de-allocate memory for an object created by the constructor.

Destructor is automatically called when the object is destroyed.

Destructor method will be called as soon as there are no other references to a particular object.

Destructor are used to de allocate memory for those variables or resource which are not longer used for php script.

Destructor are defined with the __destruct keyword.

Destructor do not return any value.

Example:


<?php 

		class Destructexample {
		
			public function __construct() {
			
			  echo  "Calling the constructor."."<br>";
			  $this->name = "object";
			  
			}
			public function __destruct() {
			
			  echo "Calling  Destructor " . $this->name ."<br>";
			  
			}
		}
		
		$obj = new Destructexample();

?>


------------ ****** ------------

OUTPUT:
        Calling the constructor.
		Calling Destructor object.