An example of php object-oriented_PHP tutorial

WBOY
Release: 2016-07-21 15:30:47
Original
1070 people have browsed it

Copy code The code is as follows:

class person{
//The following are the member attributes of person
var $name;
//The person’s name
var $sex;
//The person’s gender
var $age;
//The person’s age
// 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 given to the member attribute$ this->name is assigned an initial value
$this->name=$name;
//$sex passed in through the constructor is assigned an initial value to the member attribute $this->sex
$ this->sex=$sex;
//The $age passed in through the constructor is assigned an initial value to the member attribute $this->age
$this->age="$age";
}
//The following is the member method of the 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 person is walking";
}
//This is a destructor, which calls
function __destruct(( )
{
echo "Goodbye".$this->name."
";
}
}
//Create 3 objects $p1 through the constructor method, $p2, $p3, respectively pass in three different actual parameters: name, gender and age
$p1=new person("Xiao Ming", "Male", 20);
$p2=new person(" Bear","Female",30);
$p3=new person("Sunflower","Male",25);
//The following accesses the speaking methods of 3 objects$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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323184.htmlTechArticleCopy the code as follows: ?php class person{ //The following are the member attributes of the person var $name; // The person's name var $sex; //The person's gender var $age; //The person's age//Define a constructor parameter...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template