Destructors in Oops

In this tutorial, you come to know what is a destructor & how useful to use it in a class. This is a very important method for each class in PHP.



 

A destructor is used to clean up the resources when the object is deleted.

PHP – Destructors in Oops

A destructor allows you to clean resources after the object is deleted or the script is out of scope. It is also called the magic function of PHP, it is called as __destruct().  The destructor is automatically invoked before an object is deleted.

See the below example of how to call the destructor.

Destructor never takes any arguments, They don’t have any types or return value. It is just called before de-allocating memory for an object or after finishing of execution of PHP Script.



 

Questions asked in PHP Interview on Destructor

  1. What is a destructor in PHP
  2. What is a constructor in PHP