Object-oriented features PHP object-oriented example

Source: Internet
Author: User
Copy CodeThe code is as follows:


Class person{
The following is the person's member property
var $name;
Man's name
var $sex;
Man's Sex
var $age;
The age of the person
Define a constructor method parameter for name $name, gender $sex and age $age
function __construct ($name, $sex, $age) {
The $name passed in by the constructor method assigns the initial value to the member property $this->name
$this->name= $name;
The $sex passed in by the constructor method assigns the initial value to the member property $this->sex
$this->sex= $sex;
The $age passed in by the constructor method assigns the initial value to the member property $this->age
$this->age= "$age";
}
The following is a member method of the person
function Say ()
The way this man can talk.
{
echo "My name is:" $this->name. " gender; ". $this->sex." My age is: ". $this->age."
";
}
function run ()//The way this person can walk
{
echo "This man is walking";
}
This is a destructor that is called before the object is destroyed.
function __destruct ()
{
echo "Good-bye" $this->name. "
";
}
}
Create 3 objects by construction method $p1, $p 2, $p 3, passing in three different arguments for name gender and age, respectively
$p 1=new person ("xiaoming", "male", 20);
$p 2=new person ("Bear", "female", 30);
$p 3=new person ("Sunflower", "male", 25);
The following access 3 objects of the speaking manner $p1->say (); $p 2->say (); $p 3->say ();
?>


The output is:
My name is: Xiao Ming sex; Male my age is: 20
My name is: Bear sex; female my age is: 30
My name is: Sunflower sex; Male my age is: 25
Bye, Sunflower.
Goodbye, Bear.
Bye, xiaoming.

The above describes the object-oriented features of PHP object-oriented example, including the object-oriented features of the content, I hope to be interested in PHP tutorial friends helpful.

  • 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.