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.