<?php/** * PHP Object-oriented Programming simple instance */date_default_timezone_set ("PRC");/** 1. Static properties are used to hold the class's public data * 2. Static methods can only access static properties * 3. Static members can be accessed without instantiating an object * 4. The inside of a class can access its own static member * 5 through the self or the static keyword. You can access static members of the parent class by using the parents keyword * 6. You can access static members outside the class definition through the name of the class **/class human{public $name; protected $height; Only the public $weight can be accessed by itself and subclasses; Private $isHungry = true; Cannot access public static $sValue = "Static value in Human class". " \ n "; Public function Eat ($food) {echo $this->name. ' S eating "." ' $food "." \ n "; Public Function info () {echo "HUMAN:". $this->name. ";". $this->height. ";". $this->ishungry. " \ n "; The definition of the}}class animal{}//class begins with the keyword class, followed by the name of the class, usually the first letter of the class, starting and ending with brackets///in PHP, using the extends keyword to represent the class's inheritance, followed by the class name of the parent class// PHP extends can only follow the class name of a class, which is the single-Inheritance principle in PHP class Nbaplayer extends human{//public $name = "Jordan";//define Properties//Public $height = "198 CM ";//Public $weight =" 98kg "; Public $team = "Bull"; Public $playernumber = "23"; Private $age = "40"; Private class members can only be internally accessed//static attributes are defined with the static keyword appended to the access control keyword public static $president = "David Stern";//static methods when defined, add the static keyword after the access control keyword to public static function Changepresident ($n EWPRESDT) {//when using a static member in a class definition, use the self or the static keyword followed by: the operator can//note that when accessing the static member property:: The following is required with the $ symbol self:: $president = $NEWPRESDT;//Use P The Arent keyword accesses static members in the parent class echo Parent:: $sValue. " \ n "; The}//constructor, which automatically calls function __construct ($name, $height, $weight, $team, $playernumber) {echo "in Nbaplayer con) when an object is instantiated Stuctor\n "; $this->name = $name; $this is a pseudo-variable inside PHP that represents the object itself and can access the object's properties and methods through $->this $this->height = $height; $this->weight = $weight; $this->team = $team; $this->playernumber = $playernumber; echo $this->height. " \ n "; The}//destructor, which is called automatically at the end of program execution, is typically used to clean up resources used by the program. For example, if the program uses a printer, you can release the printer resource function __destruct () {echo "destroying" in the destructor. $this->name. " \ n "; }//Definition method Public Function run () {echo "running\n"; } Public Function jump () {echo "jumping\n"; } public FunctioN Dribble () {echo "dribbling\n"; } public Function shoot () {echo "shooting\n"; } Public Function Dunk () {echo "dunking\n"; } Public Function Pass () {echo "passing\n"; } public Function Getage () {echo $this->name. ' S age is ". ($this->age-2). " \ n "; The}}//class-to-Object instantiation//class object is instantiated with the keyword new, followed by the class name and a heap of parentheses//$jordan = new Nbaplayer ("Jordan", "198cm", "98kg", "Bull", "23");//$ James = new Nbaplayer ("James", "203cm", "120kg", "Heat", "6");//The member property of the object is accessed//through the symbol--the static property is accessed outside the class definition and can be added with the class name: operator to access static members of the class//echo Nbaplayer:: $president. " Before change "." \ n ";//nbaplayer::changepresident (" Aadam siver ");//echo Nbaplayer:: $president." \ n ";//echo Human:: $sValue." \ n ";//echo" Jordan: ". $jordan->president." \ n "//echo" James: "$james->president." \ n ";//echo $jordan->name." \ n ";//echo $jordan->getage ();//$jordan->info ();//$jordan->eat (" Apple "); The properties and methods defined in the parent class can be accessed directly on the object in the subclass//object by means of the symbol----access//$jordan->dribble ();//$jordanDunk ();//$jordan->jump ();//$jordan->pass ();//$jordan->run ();//$jordan->shoot ();////// Each time the object is instantiated with new, the constructor is called with the argument list following the class name//$james = new Nbaplayer ("James", "203cm", "120kg", "Heat", "6"),//echo $james Name. " \////////////By setting the variable to NULL, you can start the destructor call////When the object is no longer in use, it will trigger the destructor//$james 1 = $james;//$james 2 = & $james;//$james 2 = null ;//$james 1 = null;//echo "From now on James would not be used.\n";
Related recommendations:
JavaScript Object-oriented examples
PHP Object-oriented instance of identity object
JS Object-Oriented inheritance knowledge detailed