How to Create a Class Destructor in PHP



PHP




In this article, we show how to create a class destructor in PHP.

A destructor is a method in a class that is called when an object is deleted.

If an object is being deleted, then PHP automatically calls the destructor method.

We can do do anything at this time, including echoing out a message that the object is being deleted. We can close a database connection. Or because a destructor is called when an object is being deleted, if we are keeping track of the number of objects in a class, decrease the count by 1.

So the PHP __destruct() function allows us to perform any action when an object is deleted.

To create a destructor method in PHP, you use the public keyword, followed by the word function, followed by __destruct(). 2 underscores (__) precedes the word destruct().

This is shown below.



Below is a PHP program that contains a destructor method that outputs that the object is being deleted and decrements the count of a variable that stores the number of objects in a class. This is because an object is being deleted when a destructor is called, so we want to decrement the count of the objects by 1.



So we create a class named cars.

In the first line of this class, we define and initialize a static $count variable. This variable will keep hold of the number of objects in the class.

We create a constructor method, which is a method that is called when an object is instantiated. Since the objects represent cars, 2 parameters that we pass into the constructor method are the $type and $year of the car. Based on the values passed in, we store these values into the $type and $year variables using the $this keyword. We then access the static count variable and increment it by 1. This is because the constructor is called only when a new object is instantiated.

We then create the destructor method. A destructor method never accepts any arguments. So it's always destruct().

In this destructor method, we output that the object is being deleted. We also have the static property $count decrement by 1. Because this variable keeps track of the number of objects in a class and an object is now being deleted, we decrement the count by 1.

Below is a list of 5 objects we create that represent cars. For each object that is created, the constructor method is called. This assigns the type and year of each car. It also increments the static $count property so that it now holds the value 5 (due to 5 objects).

We then delete the $car4 object using the PHP unset() function. When this occurs, the PHP __destruct() function is automatically called. This function outputs that the object is being deleted. We also decrement the static $count property by 1, so that it now has a value of 4.

And this is all that is required to make a destructor method in PHP.

Again, a destructor method is called whenever an object is deleted using the PHP unset() function. It also will be called after a script ends.

And this is all that is required to make a destructor method in PHP.

Running the PHP code above yields the following output shown below.

Actual PHP Output


The Tesla Model X is being deleted
The number of objects in the class is 4


Related Resources

How to Create a Class Constructor in PHP

How to Delete an Object in PHP

HTML Comment Box is loading comments...