General function variables are released after the function ends, such as local variables, but static variables do not. The value of the variable is preserved the next time the function is called.
Basic usage of static variables
1. Defining static variables in a class
[Access modifier] static $ variable name;
2. How to access static variables
If there are two methods to access in a class self::$ static variable name, class Name:: $ static variable name
If you are accessing outside the class: There is a method class name:: $ static variable name
classchild{ Public $name; //this defines and initializes a static variable $nums Public Static $nums=0; function__construct ($name){ $this->name=$name; } Public functionJoin_game () {//Self :: $nums using static variablesSelf::$nums+=1; Echo $this->name. " Join Snowmen Game "; } } //Create three children$child 1=NewChild ("Li Kui"); $child 1-join_game ();$child 2=NewChild ("Zhang Fei"); $child 2-join_game ();$child 3=NewChild ("Tang Monk"); $child 3-join_game ();//See how many people are playing gamesEcho"<br/> has this". Child::$nums;
PHP--Static variables