Change the value of an internal property by using a function:
1<?PHP2 3 classhuman{4 Public $name;5 protected $height; 6 Public $weight;7 Private $isHungry=true; 8 9 }Ten One A classNbaplayerextendshuman{ - - Public $team= "Bull"; the Public $playerNumber= "23"; - - Public $president= "David Stern"; - + Public functionChangepresident ($NEWPRSDT) { - $this->president =$NEWPRSDT; + } A } at - $jordan=NewNbaplayer ("Jordan", "198cm", "98kg", "Bull", "23"); - $james=NewNbaplayer ("James", "203cm", "120kg", "Heat", "6"); - - $jordan->changepresident ("Adan Silver"); - Echo"Jordan:".$jordan->president. " <br/> "; in Echo"James:".$james->president. " <br/> ";
At this point, by $jordan->changepresident ("Adan Silver") , to change Jordan's president,
How to change the value of the President of Jordan and James at the same time, this will require the use of static variables
1<?PHP2 3 classhuman{4 Public $name;5 protected $height; 6 Public $weight;7 Private $isHungry=true; 8 9 }Ten One A classNbaplayerextendshuman{ - - Public $team= "Bull"; the Public $playerNumber= "23"; - //static property Definitions Add the Static keyword after the access control keyword to - Public Static $president= "David Stern"; - //static methods are actually preceded by static function + Public Static functionChangepresident ($NEWPRSDT) { - //when a class definition uses a static member, follow the Self keyword followed by:: operator + //When accessing static member properties:: followed by the $ symbol ASelf::$president=$NEWPRSDT; at } - } - //To access static properties outside the class definition, we can use the class name plus: operator to access the static members of the class - EchoNbaplayer::$president. "Before changed". " <br/> "; -Nbaplayer::changepresident ("Adam Silver"); - EchoNbaplayer::$president. "After changed". "<br/>";
Output Result:
1 David Stern before changed 2 Adam Silver after changed
Then we'll discuss the static variables of the subclass static function call Parent class
1<?PHP2 3 classhuman{4 Public $name;5 protected $height; 6 Public $weight;7 Private $isHungry=true; 8 Public Static $sValue= "Static value in Human class". " <br/> ";9 Ten } One A classNbaplayerextendshuman{ - - Public $team= "Bull"; the Public $playerNumber= "23"; - Private $age= "40"; - //static property Definitions Add the Static keyword after the access control keyword to - Public Static $president= "David Stern"; + //static methods are actually preceded by static function - Public Static functionChangepresident ($NEWPRSDT) { + //when a class definition uses a static member, follow it with the self or the static keyword:: operator A //When accessing static member properties:: followed by the $ symbol atSelf::$president=$NEWPRSDT; - //static members of the parent class can be accessed using the parent keyword - EchoParent::$sValue; - //static functions do not have access to dynamic properties - //echo->age; This is going to go wrong - } in } - //To access static properties outside the class definition, we can use the class name plus: operator to access the static members of the class to EchoNbaplayer::$president. "Before changed". " <br/> "; +Nbaplayer::changepresident ("Adam Silver"); - EchoNbaplayer::$president. "After changed". "<br/>"; the //output The static variables of the parent class * EchoHuman::$sValue;
The output is:
David Stern before changed Static class Adam Silver after changed Static class
Summed up on six points:
1, static property is used to save the class's public data 2, static methods can only access static property 3, static members do not need to instantiate the object can access 4, The inside of a class can access its own static member 5 through the self or the static keyword, and you can access static members of the parent class by using the Parental keyword 6, which can be accessed outside the class's definition by the name of the class
static (statically) keyword