What is destructoer in php?
Destroying object :-
The object variables that you create are removed from system memory when the requested page has completed running. When the variable falls out of scope, or when it is explictly set to null. In php 6 , you can trap the destruction of the object and take actions when that happens. To do create a function called __destruct( ) with no parameters. Before the object is destroyed, this function is called automatically, if it exists.
Fetches the properties of an object from a database. If any properties of the object are changed, they are automatically saved back to the database when the object is destroyed. This eliminates the need to explicitly call a save method. The destructor also closes the open database connection handle.
A destructor is a special function of a class that is automatically executed whenever an object of a class is destroyed.
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.
File name : index.php
<?php
class WidgetTest
{
private $id;
private $name;
private $description;
private $hdb;
private $needsupdating = false;
public function __construct($wid)
{
$this->hdb = mysql_connect("localhost","root",'');
if(! is_resource($this->hdb))
{
throw new Exception('unable to connect to database');
}
$qry = "select * from user_info where userid=$wid";
$result = mysql_query($this->hdb,$qry);
if(!is_resource($result))
{
throw new Exception("an error occured selecting from database");
}
if(!mysql_num_rows($result))
{
throw new Exception("the specified widget does not exist");
}
$data = mysql_fetch_array($result);
$this->$wid;
$this->name = $data['name'];
$this->description = $data['description'];
}
public function getName()
{
return $this->name;
}
public function getDescription()
{
return $this->description;
}
public function setName($name)
{
$this->name=$name;
}
public function setDiscription($discription)
{
$this->description = $discription;
}
File name : Testcall.php
<?php
require_once 'widget.php';
try{
$obj = new WidgetTest(1);
print "widget name : " . $obj->getName() ."<br>\n";
print "widget description : " . $obj->getDescription() ."<br>\n";
}
catch(Exception $e)
{
die ("there was a problem" .$e->getMessage());
}
?>
The two accessor methods, getName( ) and getDescription( ), enable you to fetch the values of the private member variables. Similarly the setName( ) and setDescription( ) methods enable you to assign a new value to those variables.
File name : index.php
A PHP5 destructor is defined by implementing the __destruct() method. In PHP4 however, the concept of a destructor did not exist.
A destructor cannot take any arguments.
Let’s look at how to define a PHP5 Destructor
class Customer { public function __destructor() { //code } }
File name : index.php
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”;}
}
File name : index.php
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 = “P0001″;$c1 =new Customer();$c1->setData(“Stuart”,”Broad”,0);
$o = new Order($order_id, $c1);
In the above example, we create a new object 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 and hence the destructor is automatically called.
Destructor :-
Php also support destructors. Which are called whey you destroy an object. You use destructors to clean up after an object. Terminating database or internet connections.
In php, destructors are called when you explictly destroys an object, or when all references to the object go out of scope.
Destructors are named __destruct( ).
Syntax
function __destruct( )
{
}
File name : index.php
<?php
class Person
{ var $name;
function __construct($data) {
echo "constructing ", $data, "...<br>";
$this->name=$data;
}
function set_name($data) {
$this->name=$data;
}
function get_name() {
return $this->name;
}
function __destruct() {
echo "Destructing ", $this->name, "....<br>";
}
}
$obj = new Person("mahtab");
echo "Your Name is ", $obj->get_name(), ".";
?>
Previous
Next