Deleting Objects with unset()

To delete an object before the end of a script the keyword unset() is used with the object’s variable name within its parentheses

 

Syntax:

unset($myObject);

 

To call the destructor before the end of the script, i.e. when you require, use the unset() function:

Save & refresh browser:

An instance of the the ‘Dog’ class has been instantiated!
Yalp!! The ‘Dog’ object has been destroyed!
The script ends here.

 

It can now be seen that the destructor has been called before the end of the script by using the unset() function.

Leave a Reply