The seventh chapter (4) Object-oriented analysis----construction method and deconstruction method
The use of special reference $this
Example Description:
Class Ren
{
var $xingming;
var $nianling;
var $xingbie;
function Shuohua ()
{echo "My name is:". $this->xingming. ", My Age is:". $this->nianling. ", My gender is:". $this->xingbie. " <br> ";}
function Zoulu ()
{echo "I'm walking <br>";}
function Chifan ()
{echo "I'm eating <br>";}
}
$r 1=new Ren (); $r 2=new Ren (); $r 3=new Ren ();
$r 1->xingming= "John"; $r 2->xingming= "Dick"; $r 3->xingming= "Harry";
$r 1->nianling=90; $r 2->nianling=23; $r 3->nianling=41;
$r 1->xingbie= "Male"; $r 2->xingbie= "female"; $r 3->xingbie= "Male";
People 1.2.3 their names. Age. Sex:
$r 1->shuohua (); $r 2->shuohua (); $r 3->shuohua ();
Output results:
My name is: John, my age is: 90, my sex is: male
My name is: Dick, my age is: 23, my sex is: female
My name is: Harry, my age is: 41, my sex is: male
The role of $this: Represents an attribute of an object, equivalent to the first person "I" concept.
The characteristics of the construction method: The name is the same as the class, the object is automatically invoked when it is generated, to initialize the member properties.
Example Description: (this instance is the declaration method inside the PHP4)
Class Ren
{
var $xingming;
var $nianling;
var $xingbie;
function Ren ($xingming, $nianling, $xingbie)
{
$this->xingming= $xingming;
$this->nianling= $nianling;
$this->xingbie= $xingbie;
}
function Shuohua ()
{echo "My name is:". $this->xingming. ", My Age is:". $this->nianling. ", My gender is:". $this->xingbie. " <br> ";}
function Zoulu ()
{echo "I'm walking <br>";}
function Chifan ()
{echo "I'm eating <br>";}
}
$r 1=new Ren ("John", 90, "male");
$r 2=new Ren ("Dick", 23, "female");
$r 3=new Ren ("Harry", 41, "male");
$r 1->shuohua (); $r 2->shuohua (); $r 3->shuohua ();
This example shows the same output in the previous example.
The construction method in the PHP5 version is Function__construct (), which works the same without writing the class name.
destructor __destruct () The method of releasing what is to be released before the end of the program.
Example Description:
Class Ren
{
var $xingming;
var $nianling;
var $xingbie;
function __construct ($xingming, $nianling, $xingbie)
{
$this->xingming= $xingming;
$this->nianling= $nianling;
$this->xingbie= $xingbie;
}
function Shuohua ()
{echo "My name is:". $this->xingming. ", My Age is:". $this->nianling. ", My gender is:". $this->xingbie. " <br> ";}
function Zoulu ()
{echo "I'm walking <br>";}
function Chifan ()
{echo "I'm eating <br>";}
function __destruct ()
{echo $this->xingming. " Goodbye <br> ";}
}
$r 1=new Ren ("John", 90, "male");
$r 2=new Ren ("Dick", 23, "female");
$r 3=new Ren ("Harry", 41, "male");
$r 1->shuohua (); $r 2->shuohua (); $r 3->shuohua ();
Output results:
My name is: John, my age is: 90, my sex is: male
My name is: Dick, my age is: 23, my sex is: female
My name is: Harry, my age is: 41, my sex is: male
Harry Goodbye Dick Goodbye John Goodbye