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





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

	class Dog {

		public function __construct() {
			echo "An instance of the the '" . __CLASS__ . "' class has been instantiated!<br>";
		public function __destruct() {
			echo "Yalp!! The '" . __CLASS__ . "' object has been destroyed!<br>";
	$fido = new Dog;
	echo "The script ends here.<br>";

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