PHP Oops Destructor - PHP Constructors and Destructors



PHP Destructor

  • A PHP Destructor is a special type of function that gets called when a script ends or an object is destroyed.
  • It is used to clean up any resources that have been allocated to an object during it's lifetime, such as closing database connections or freeing up memory.
  • It is also used to perform any final clean up operations that need to be done.
php-constructor

Sample Code

<?php
class Car {
  public $name;
  public $color;

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

$audi = new Car("Audi", "red");
?>

Output

The Car is Audi and the color is red.

Related Searches to PHP Oops Destructor - PHP Constructors and Destructors