The PHP global variable uses the keyword global declaration, static variables use static declarations, and the use of static variables can use the class name:: Variable name
Example code:
1<?PHP2 3 //usage of global variables and use of static variables4 Global $sum;5 $sum=0;6 class Person7 {8 Public $name;9 Public $age;Ten Public Static $zong=0; One Public function__construct ($name,$age) A { - $this->name=$name; - $this->age=$age; the } - Public functionAddNew () - { - Echo $this->name. " Join the team! <br/> "; + Global $sum; - $sum++; +Self::$zong++; A } at Public function__destruct () - { - Echo"<br/>".$this->name. " Leave the team! <br/> "; - } - } - $p 1=NewPerson ("Zhang San", 12); in $p 1-AddNew (); - $p 2=NewPerson ("John Doe", 13); to $p 2-AddNew (); + Echo"Shared".$sum." People join the team <br/> "; - Echo"<br/><br/><br/><br/>"; the Echo"The record value for the static variable is". Person::$zong; * $ //usage of static variable globalPanax Notoginseng -?>
View Code
Examples of use of static methods:
1<?PHP2 classStudent3 {4 Public Static $sum=0;5 Public $fee;6 Public $name;7 Public function__construct ($name,$fee)8 {9 Echo $name." Join the school, spend ".$fee." Yuan <br/> ";Ten $this-$fee=$fee; One $this-$name=$name; ASelf::$sum+=$fee; - } - Public Static functionGetfee () the { - returnSelf::$sum; - } - } + $st 1=NewStudent ("Zhang San", 12); - $st 2=NewStudent ("John Doe", 13); + Echo"School total income (class name static method call)". Student::getfee (). " Yuan <br/> "; A Echo"School total income (instance static method invocation)".$st 1->getfee (). " Yuan <br/> "; at?>
View Code
"PHP Global variables and static variables, how to use static methods"