Static variables and static methods in PHP

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.