1, static variables: variables shared by all objects become static variables. A static variable is similar to a global variable, but a global variable destroys the encapsulation of an object, so it corresponds to a process-oriented, static variable that corresponds to an object-oriented.
2, global variables, the use of global variables as follows, declare global variables at the time of Globals $global _nums; When using global variables in local functions, it is necessary to declare the following global variables, and declare that when the system creates a variable in the stack, the variable holds the address, which points to the global variable of the global zone.
<?php/** * Created by Phpstorm. * User:usa007lhy * DATE:2015/10/15 * time:21:38 */global $global _nums; $global _nums = 0;class child{public $name;
function __construct ($name) { $this->name = $name; } Public Function Join_game () { global $global _nums; $global _nums++; echo $this->name. " Join to play snowball game <br/> "; }} $child 1 = new Child (' small just '); $child 1->join_game (); Echo $global _nums. " The individual joins the heap snowball game. <br/> "?>
When the program changes, the memory changes as shown
3, static variable
Static variables can only be accessed by the class, there are two ways to access static variables, (1) class name:: Static variable, any position can be used, (2) Self:: Static variable, can only be used within the class, that is, only for member functions.
<?php/** * Created by Phpstorm. * User:usa007lhy
* Author:www1.qixoo.com
* DATE:2015/10/15 * time:21:38 * */class child{public static $nums = 0, public $name; function __construct ($name) {$this ->name = $name; } public Function Join_game () {self:: $nums + +; echo $this->name. " Join to play snowball game <br/> "; }} $child 1 = new Child (' small just '); $child 1->join_game (); echo Child:: $nums. " The individual joins the heap snowball game. <br/> "?>
When the program runs, the memory changes as follows:
4, static method
Static variables and static methods in PHP