Object-oriented PHP (1)

Source: Internet
Author: User
: This article mainly introduces object-oriented PHP (1). If you are interested in the PHP Tutorial, refer to it.

  Name. "is eating". $ food ."
";}}/**** NbaPlayer class definition */classNbaPlayerextendsHuman {// extends: indicates Inheritance. in php, extends can only follow the class name of one class (single inheritance principle) // attribute public $ team = "PTS"; public $ playerNum = "1221"; private $ age = 12; // The Static attribute defines publicstatic $ president = "David "; // static method definition publicstaticfunctionchangePresident ($ newPresident) {static: $ president = $ newPresident; // self: $ president = $ newPresident; // use static members in the class definition and use static or self: static member variable // use the parent keyword to access the static member echoparent ::$ staticValue of the parent class."
";}// Construct function _ construct ($ name, $ weight, $ team, $ playerNum) {echo" executes the constructor...
"; $ This-> name = $ name; // this is a pseudo variable in php, and $ this-> weight = $ weight; $ this-> team = $ team; $ this-> playerNum = $ playerNum;} // destructor function _ destruct () {echo "execute destructor...
";}// Define the method publicfunctionrun () {echo" Runing... \ n ";} publicfunctionjump () {echo" Jumping... \ n ";} publicfunctiondribble () {echo" Dribbling... \ n ";}publicfunctionshoot () {echo" Shooting... \ n ";} publicfunctiondunk () {echo" Dunking... \ n ";}publicfunctionpass () {echo" Passing... \ n ";} publicfunctiongetAge () {echo $ this-> name. "This Year ". $ this-> age. "years old.
";}}// Class instantiation $ pzy = new NbaPlayer (" Peng Zhongyao "," 182 "," 75 kg "," PTS "," 1221 "); $ pts = new NbaPlayer ("Peng Xiaoyao", "128", "15 kg", "PTS", "1221"); echo $ pzy-> name. "\ n"; echo $ pzy-> eat ("Big Watermelon"); // echo $ pzy-> age; $ pzy-> getAge (); // echo $ pzy-> name. "The alliance's president is ". $ pzy-> president; // x cannot access static members // echo $ pts-> name. "The alliance's president is ". $ pts-> president; // static member for access outside the class definition: class name: static member variable echo "previous ". nbaPlayer: $ president."
"; NbaPlayer: changePresident (" Peng Dayao "); echo" is ". NbaPlayer: $ president ."
";?>

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

The above introduces the object-oriented PHP (1), including the content, hope to be helpful to friends who are interested in PHP tutorials.

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.