Comparison of static member variables and normal member variables for PHP classes

Source: Internet
Author: User

There are a lot of people who may not know what is the difference between a static member variable and a normal member variable in a class in PHP?

1. Static methods of a class can access only static member variables, not non-static member variables

2, static member variables do not need to be instantiated to access, and access faster

3. Static member variables of a class belong to this class only, but all instances of the class share this static member variable


A program that has been compiled by C + + has divided the memory into a few parts

(1), stack area stack:

The compiler automatically allocates releases, stores the function's parameter values, the values of local variables, and so on. This stack works like a stack in a data structure. The stack area is allocated well before the program runs, faster than the heap

(2), heap area heap:

Usually released by the programmer, if the programmer does not release, the program ends may be recycled by the OS, note that it is different from the heap in the data structure, distribution is similar to the list. is dynamically allocated when the program is running, slower than the stack

(3), Global zone (Static go) static:

The storage of global variables and static variables is placed in one piece. Initializes global variables and static variables in one area, and uninitialized global and static variables are placed in another adjacent area. Released by the system after the program is finished.

(4), text constant area:

The constant string is placed here. Released by the system after the program is finished.

(5), Program code area:

2 binary code that holds the function body.


Let's do a little example together:

Class a{public    static $a = 5;    Public $b = 4;    Public Function Geta () {        return A:: $a;    }    Public Function SetA ($val) {        A:: $a = $val;    }    Public Function Getb () {        return $this->b;    }    Public Function Setb ($val) {        $this->b = $val;    }    public static function Getbb () {        return $this->a;    }}

$a = new A (); Echo $a->geta (); $a->seta (' aaaa '); Echo ' <br/> '; $b = new A (); Echo $b->geta (); Echo ' <br/> '; $c = new A (); Echo $c->geta ();
The above results will be: 5 aaaa AAAA

The object $ A of the class modifies the static member variable value of the class, because all instance objects of the class share static member variables, so other objects change when they get the value of the static member variable.

$a = new A (); Echo $a->getb (); $a->setb (' aaaa '); Echo ' <br/> '; $b = new A (); Echo $b->getb ();

The above results are: 4 4

Echo A::GETBB ();

An error occurs because the static method of the class can only access static member variables

I'm sure you'll understand when you see this!

The wrong place is welcome to point out!


Comparison of static member variables and normal member variables for PHP classes

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.