Constructor And Destructor
PHP : Constructor And Destructor :
Php
constructors(version 4) :
1.
A
constructor is a function .
2.
Name
of the constructor function is same as that of class name.
3.
A
constructor function will gets automatically called when an object of a class
is created.
4.
A
constructor is used to initialize the member datas of an object.
Php
Constructor(version 5) :
1.
In
php5 a constructor is defined by implementing
_constructor() method, when an object is created , php5 searches for
__construct() first. If __construct is not defined them it searches for a
method with a name same that of the class.
Php5 destructor :
A destructor is a
special function of a class that is automatically executed whenever an object
of a class is destroyed.
An object of a class is destroyed when :
1.
The program execution is over.
2.
It goes out of scope.
3.
You specifically set it to null.
4.
You unset it.
A php5 destructor is
defined by implementing the __destruct() method.
<?php
class constrdestr
{
private $empno,$ename,$sal;
//php4 constructor
function constrdestr($a=0,$b="na"
,$c=0)
{
echo "******<br>";
$this->empno=$a;
$this->ename=$b;
$this->sal=$c;
}
//php5 constructor
function __construct($a=0,$b="na"
,$c=0)
{
echo "??????<br>";
$this->empno=$a;
$this->ename=$b;
$this->sal=$c;
}
//php5 destructor
function __destruct() {
echo "inside
destructor<br>";
}
function getData()
{
echo
$this->empno."--".$this->ename."--".$this->sal."<br>";
}
}
?>
<?php
echo
"<h1>";
$emp1 =new
constrdestr();
$emp2 =new
constrdestr(101,"penna");
$emp3 = new constrdestr(102,"lpo",123.45);
$emp1->getData();
$emp2->getData();
$emp3->getData();
unset($emp1);
$emp2 =11;
$emp3=NULL;
echo "application
completed!!!";
?>
OutPut:
??????
??????
??????
0--na--0
101--penna--0
102--lpo--123.45
inside destructor
inside destructor
inside destructor
application
completed!!!
0 comments:
Post a Comment