Destructor in Php


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:

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("Hiren","Prajapati",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)

No comments: