Home >Backend Development >PHP Tutorial >Characteristics of object-oriented php An example of object-oriented

Characteristics of object-oriented php An example of object-oriented

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-29 08:44:401014browse

Copy the code The code is as follows:


class person{
//The following are the member attributes of the person
var $name;
//The person’s name
var $sex;
//Person Gender
var $age;
//Age of person
//Define a constructor parameter as name $name, gender $sex and age $age
function __construct($name,$sex,$age){
// The $name passed in through the constructor is assigned an initial value to the member attribute $this->name
$this->name=$name;
//The $sex passed in through the constructor is assigned to the member attribute $this-> Sex is assigned an initial value
$this->sex=$sex;
//The $age passed in through the construction method is assigned an initial value to the member attribute $this->age
$this->age="$age" ;
}
//The following is the member method of person
function say()
//The method by which this person can speak
{
echo "My name is: ".$this->name."Gender;". $this->sex."My age is: ".$this->age."
";
}
function run() //How this person can walk
{
echo "This People are walking";
}
//This is a destructor, called before the object is destroyed
function __destruct()
{
echo "Goodbye".$this->name."
";
}
}
//Create three objects $p1, $p2, $p3 through the construction method, and pass in three different actual parameters for name, gender and age respectively
$p1=new person("Xiao Ming", "male" ,20);
$p2=new person("Bear","Female",30);
$p3=new person("Sunflower","Male",25);
//The following is the speech of the three objects visited Method $p1->say();$p2->say();$p3->say();
?>


The output result is:
My name is: Xiao Ming Gender; Male My age is: 20
My name is: Bear Gender; Female My age is: 30
My name is: Sunflower Gender; Male My age is: 25
Goodbye Sunflower
Goodbye Bear
Goodbye Xiao Ming

The above introduces the object-oriented characteristics of PHP and an example of object-oriented, including the characteristics of object-oriented. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn