An object-oriented explanation of PHP--construction method and destructor method

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.