PHP Destructor

PHP Destructor


Destructor is a special function. Destructor in PHP is similar to other oops(object oriented programming) concept.A destructor is called when the object is destructed or the script is stopped or exited.If you create a __destruct() function, PHP will automatically call this function at the end of the script.
In some programming languages, objects have to be manually destroyed, but in PHP this work is done by the garbage collector. As soon as an object is free and it is no longer needed, the garbage collector destroys it.
 
The example below has a __construct() function that is automatically called when you create an object from a class, and a __destruct() function that is automatically called at the end of the script:

Output : My name is Alex

Example 2 : similar but passing two arguments.

Output : My name is Alex and age is 25