Thursday 8 October 2020

What is destructor? explain with example in php

 DESTRUCTOR

  • A destructor is called when the object is destroyed.
  • A destructor is a special method called automatically during the destruction of an object. Actions executed in the destructor include the following
    • Recovering the heap space allocated during the lifetime of an object
    • Closing file or database connections
    • Releasing network resources
    • Releasing resource locks
    • Other housekeeping tasks

Example of Constructor and Destructor:
<?php
class dclass
{
    public $name = "Youtube with Avadh Tutor";    
    public function __construct($name)
    {
        echo "<font color=blue>By default Constructor</font>";    
        $this->name = $name;
    }
    
    public function __destruct()
    {
        echo "<font color=orange>Class with destructor Call </font>";
//$this->name = $name; /*error*/
    }
}

$d = new dclass("Argument");
echo "<br><br>Name of the Channel: " . $d->name;
/* destruct call after object destroy*/
?>

No comments:

Post a Comment