How to Delete an Object In PHP 


               


How to Delete an Object in PHP



PHP




In this article, we show how to delete an object in PHP.

An object is an instance of a class.

Using the PHP unset() function, we can delete an object.

So with the PHP unset() function, putting the object that we want to delete as the parameter to this function, we can delete this object.

This is shown below.



So in the above code, we delete an object named $object1.

In the code below, we show a complete example of deleting an object.



So in the code above, we create a class named cars.

Inside of this, we declare one property named $type. This will represent the type of car it is.

We then instantiate an object of this class cars, $car1.

We assign the $car1 object the type, Hyundai Elantra.

We then delete the $car1 object through the unset() function.

It's very simple.




Creating a Destructor Method

You also have the option of puting a destructor method in a PHP class.

When an object is deleted, the destructor method is automatically called and you can do anything in this code, just as with any method. For example, you can close a database connection. Output that the object is being deleted. If you're keeping track of the number of objects in a class, you would decrement the variable keeping count by 1 (since an object is deleted when this method is called).

This, again, is optional. You don't have to have a destructor method in your code. However, if you want to call a method when an object is being deleted, you would then want to add a destructor method because it's called whenever an object is deleted.

Below is an example code in which we add a destructor method. When we delete an object using the unset() function, this destructor method is automatically called. We otuput that the object is being deleted and since we keep track of all the objects in a class, we decrement the variable storing the count by 1.



So we create a class named cars.

In the next line, we create a static variable $count that stores the value of count. We set to equal to 0 initially because no objects have been created or deleted yet. It starts out at the neutral ground of 0.

We then create a constructor method which takes in the $type and $year variables as its arguments. When an object is instantiated, the $type and $year values get set based on the values we pass into the object. We increment the $count variable by 1, because a constructor is called when an object is created.

We then create the destructor method. In this method, we echo that the object is being deleted. We also decrement the $count variable by 1, since an object is being deleted. (A destructor method is only called when an object is being deleted0)>

After, this, we create 5 car objects. During this time, the __construct method is called. The $count variable, thus, gets incremented 5 times, having a value of 5.

We delete the $car4 object through the unset() function. It is at this time the __destruct() method is called. Thus, at this time, we output that the object is being deleted and decrement the $count variable by 1. So now the $count variable has a value of 4.

We then output the number of objects in the class, which is now 4, since 1 has been deleted.

So, again, having a destructor method in your class is optional. It's only if you want something done when an object is being deleted. This can be closing a datbase connection, keeping track of the number of objects in a class, echoing out some output, etc.

So this was a tutorial on deleting an object of a class in PHP.


Related Resources

How to Create a Class Destructor in PHP

HTML Comment Box is loading comments...