: 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.