PHP Static member variables

Source: Internet
Author: User

Static member: A member in a static class joins the static modifier, which is a statically member. You can access this static member directly by using the class name + static member name, because static members exist in memory, and non-static members need to be instantiated to allocate memory. Therefore, static members cannot access non-static members: Because static members exist in memory, non-static members can access static members of the class directly.

1. Static Global variables

Definition: Before a global variable, the variable is defined as a static global variable, plus the keyword static.

Characteristics:
A, the variable allocates memory in the global data area.
B, initialization: If it is not explicitly initialized, it is implicitly initialized to 0 (the automatic variable is random unless explicitly initialized).
C, the visit variable only in the source document is visible, strictly speaking should be the definition of the beginning to the end of this document.

2. Static local Variables

Characteristics:


C, It always resides in the global data area until the program finishes running. But its scope is a local scope, and when the function or block of statements that defines it ends, its scope ends.

Static data members comply with PUBLIC,PROTECTED,PRIVATE access rules like normal data members;

Because static data members allocate memory in the global data area, all object shares of this class are shared, so it is not part of a particular class object, and its scope is visible when no class object is produced, that is, when an instance of the class is not produced, we can manipulate it; "Static data member initialization differs from general data member initialization." The static data member is initialized in the following format:

Data type >< class name >::< static data member name >=< value >

The static data members of a class are accessed in two ways:
class object name >.< static data member name or class type name >::< static data member name
If the access permission of the static data member is allowed (that is, the member of public), the static data member can be referenced in the program according to the above format;

Static data members are used primarily when each object has the same property. For example, for a deposit class, the interest is the same for each instance. Therefore, interest should be set as a static data member of the deposit class. There are two benefits, first, regardless of how many deposit class objects are defined, the interest data members share the memory allocated in the global data area, thus saving storage space. Second, once the interest needs to be changed, the interest of all the deposit classes will change as soon as it is changed;

PHP Static member variables

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.