PHP oops - Destructor

What is PHP oops - Destructor?

A destructor is called when the object is destructed or release any object from its memory.

A destructor is used to clean up resources from the memory.

You can define destructor by using __destruct function.

A Destructor method doesn't take any arguments

A Destructor does not return any data type.

The automatic destruction of class objects is handled by PHP Garbage Collector.

A Destructor is just called before de-allocating memory for an object or during the finish of execution of PHP scripts or as soon as the execution control leaves the block.

PHP Syntax:

function __destruct()

PHP Example :

<?php
// code by aryatechno!
class Tutorials
{
    function __construct()
    {
        echo "Learn php Course  __destruct at aryatechno!";
    }
    function __destruct(){
      echo "<br>Object is destroyed";
   }
    
}

$learn = new Tutorials();

?>

Output:

Learn php Course __destruct at aryatechno!
Object is destroyed

 

 

 

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

64932