PHP OOP Destructor

When an object destructs, destructors are called. It’s usually when the script comes to an end.

The destruct function is called at the end of the script.

Note: Constructor and destructor both are used to reduce the code so they are useful.

Destructor Syntax:

The destructor function is named destruct and is followed by a double underscore.

 function __destruct() {
   // 
  }
<?php
class Car {
  // Properties
  var $name;
  var $color;

  // Methods
  function __construct($name, $color) {
    $this->name = $name;
    $this->color = $color; 
  }
  function __destruct() {
    echo "The Car is {$this->name} and the color of car is {$this->color}."; 
  }
}

$car = new Car("Civic", "Black");
?>