Home >Backend Development >PHP Tutorial >Characteristics of object-oriented php An example of object-oriented
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 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.