Home > PHP, PHP Tutorials, PHP5 OOPS Tutorials > PHP5 Tutorial OOPS – Creating a PHP5 Destructor __destruct()

PHP5 Tutorial OOPS – Creating a PHP5 Destructor __destruct()

November 14th, 2007 admin Leave a comment Go to comments

Definition of a Destructor
A destructor is a special function of a class that is automatically executed whenever an object of a class is destroyed.

What does this all mean?
Let’s revisit that definition in more simple terms. A destructor is a special function – this means that a destructor is a function; but its special. But, why is it special? It’s special because it is automatically executed or called when an object of a class is destroyed. An object of a class is destroyed when

  1. it goes out of scope,
  2. when you specifically set it to null,
  3. when you unset it or when the program execution is over.




Why do we need a Destructor?
It is needed as it provides an opportunity for doing necessary cleanup operations like unsetting internal class objects, closing database connections or socket connections, etc. In simple terms, it is needed to cleanup the object before it is destroyed. You should also read more about Garbage Collection to understand how clean up happens.

PHP5 Destructor
A PHP5 destructor is defined by implementing the __destruct() method. In PHP4 however, the concept of a destructor did not exist.

Important Note:
A destructor cannot take any arguments.

Let’s look at how to define a PHP5 Destructor

class Customer {
	public function __destructor() {
		//code
	}
}

Let’s look at a real example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class Customer {
 
	private $first_name;
	private $last_name;
	private $outstanding_amount;
 
	public function __construct() {
		$first_name = "";
		$last_name = "";
		$outstanding_amount = 0;
	}
 
	public function setData($first_name, $last_name, $outstanding_amount) {
		$this->first_name = $first_name;
		$this->last_name = $last_name;
		$this->outstanding_amount = $outstanding_amount;
	}
 
	public function printData() {
		echo "Name : " . $first_name . " " . $last_name . "\n";
		echo "Outstanding Amount : " . $outstanding_amount . "\n";
	}
 
}
 
class Order {
	private $order_id;
	private $customer;
 
	public function __construct($order_id, $customer) {
		$this->order_id = $order_id;
		$this->customer = $customer;
	}
 
	public function __destruct() {
		unset($this->order_id);
		unset($this->customer);
	}
}
 
$order_id = "L0001";
$c1 = new Customer();
$c1->setData("Sunil","Bhatia",0);
 
$o = new Order($order_id, $c1);

In the above example on line number 45, we <a>create a new object</a> of the Order class. The argument constructor of the Order class takes two parameters i.e. $order_id and $customer object. After the program completes its execution, the object goes out of scope because the program stops execution after line 45 and hence the destructor is automatically called.




In the next PHP5 tutorial you will learn about Access Specifier.

Feel free to write comments if you need more examples or if you need to ask a question on PHP5 Class. You can also subscribe to my notification service to be informed as an when a new tutorial article goes online. Subscribe Below

Related Posts on PHP5 Tutorial – Object Oriented Programming (OOPS)

  1. PHP5 Tutorial – Learn to create a PHP5 Class
  2. PHP5 Tutorial – Learn to Create a PHP5 Class Object
  3. PHP5 Tutorial – Defining Attributes of a PHP5 Class
  4. PHP5 Tutorial – Defining Methods of a PHP5 Class
  5. PHP5 Tutorial – Creating a PHP5 Constructor __construct()
  6. PHP5 Tutorial OOPS – Creating a PHP5 Destructor __destruct()
  7. PHP5 Tutorial OOPS – PHP5 Class Access Specifiers – public, private and protected
  8. PHP5 Tutorial – Magic Methods – __toString() method
  9. PHP5 Tutorial – Magic Methods – __get() and __set()
  10. PHP5 Tutorial – Magic Methods – __isset() and __unset()
  11. PHP5 Tutorial – Magic Methods – __call() method
  12. PHP5 Tutorial – Magic Methods – __autoload() method
  13. PHP5 Tutorial – Magic Methods – __sleep() and __wakeup()
  14. PHP5 Tutorial – Magic Methods – __clone() method

Categories: PHP, PHP Tutorials, PHP5 OOPS Tutorials Tags:

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/sunilb/www.sunilb.com/wp-content/plugins/all-in-one-seo-pack/aioseop.class.php:245) in /home/sunilb/www.sunilb.com/wp-content/plugins/mycaptcha/MyCaptcha.php on line 41

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/sunilb/www.sunilb.com/wp-content/plugins/all-in-one-seo-pack/aioseop.class.php:245) in /home/sunilb/www.sunilb.com/wp-content/plugins/mycaptcha/MyCaptcha.php on line 41
  1. roshan
    July 14th, 2008 at 16:57 | #1

    class Customer {

    private $first_name;
    private $last_name;
    private $outstanding_amount;

    public function __construct() {
    $first_name = “”;
    $last_name = “”;
    $outstanding_amount = 0;
    }

    public function setData($first_name, $last_name, $outstanding_amount) {
    $this->first_name = $first_name;
    $this->last_name = $last_name;
    $this->outstanding_amount = $outstanding_amount;
    }

    public function printData() {
    echo “Name : ” . $this->first_name . ” ” . $this->last_name . “\n”;
    echo “Outstanding Amount : ” . $this->outstanding_amount . “\n”;
    }

    }

  2. February 28th, 2010 at 20:02 | #2

    At the first Example: Shouldn’t the Destructor-Function be called «__destruct()» instead of «destructor»?

  1. No trackbacks yet.
Enter this code to leave comment (Sorry, but bots get me crazy :) )