Php Object-oriented example _ PHP Tutorial

Source: Internet
Author: User
Php Object-oriented example. Copy the code as follows :? Phpclassperson {The following is the member attribute var $ name of a person; the name var $ sex of a person; the gender var $ age of a person; the age of a person defines a constructor parameter. The code is as follows:


Class person {
// The following are the member attributes of a person.
Var $ name;
// Name of the person
Var $ sex;
// Gender of a person
Var $ age;
// Age of the person
// Define a constructor parameter as name $ name, gender $ sex and age $ age
Function _ construct ($ name, $ sex, $ age ){
// Assign the initial value to the member attribute $ this-> name through the $ name passed in by the constructor
$ This-> name = $ name;
// Assign the initial value of $ sex to the member attribute $ this-> sex passed in through the constructor
$ This-> sex = $ sex;
// Assign an initial value to the member attribute $ this-> age through the $ age passed in by the constructor
$ This-> age = "$ age ";
}
// The following is the member method of a person.
Function say ()
// How this person can speak
{
Echo "My name is :". $ this-> name. "Gender ;". $ this-> sex. "My age is :". $ this-> age."
";
}
Function run () // this person can walk
{
Echo "this person is walking ";
}
// This is a destructor called before the object is destroyed.
Function _ destruct ()
{
Echo "goodbye". $ this-> name ."
";
}
}
// Create three objects $ p1, $ p2, and $ p3 by using the constructor method, and input three different real parameters as name gender and age respectively.
$ P1 = new person ("James", "male", 20 );
$ P2 = new person ("Bear", "female", 30 );
$ P3 = new person ("sunflower", "male", 25 );
// How to access the following three objects: $ p1-> say (); $ p2-> say (); $ p3-> say ();
?>


Output result:
My name is James gender; my male age is: 20
My name is: Bear gender; female: 30
My name is: Sunflower gender; male's age is: 25
Goodbye sunflower
Goodbye Bear
Goodbye James

The http://www.bkjia.com/PHPjc/323184.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/323184.htmlTechArticle code is as follows :? Php class person {// The following is the member attribute var $ name of a person; // The name of a person var $ sex; // gender var $ age of a person; // age of a person // define a constructor parameter...

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.