<?php class person{//The following is a member of the person property public $name;//person's name public $sex;//gender public $age;//person's age//define a constructor method parameter for name $name, gender $se X and age $age public function __construct ($name, $sex, $age) {//the $name passed in through the constructor method assigns an initial value to the member property $this->name $this->name=$ Name The $sex passed through the construction method assigns the initial value to the member property $this->sex $this the->sex= $sex; The $age passed through the construction method assigns the initial value to the member property $this->age $this the->age= $age; echo "My name is:" $this->name. " gender; ". $this->sex." My age is: ". $this->age." <br> ";} Here are the members of the people method public function say ()//This person can speak the way {echo "My name is:". $this->name. " gender; ". $this->sex." My age is: ". $this->age." <br> "; } public Function run ()//This person can walk {echo "This person is walking";}//This is a destructor that calls public function __destruct () {echo "Goodbye" before the object is destroyed. $this ->name. " <br> "; }}//Create 3 objects by construction method $p1, $p 2, $p 3, pass in three different arguments for name gender and age $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 ();?>
PHP Object-oriented programming