PHP Object-Oriented programming

Source: Internet
Author: User
Tags php class


<?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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.