PHP static member variables

Source: Internet
Author: User
Static member: a member in a static class is added with a static modifier, which is a static member. you can directly use the class name + static member name to access this static member. because the static member exists in the memory, non-static members need to be instantiated to allocate the memory, so static members cannot access non-static members .. because static members exist in the memory, non-static members can directly access static members in the class. static members: add members in the static class StaticModifier, which is a static member. you can use it directly. Class name + static memberBecause the static members exist in the memory, non-static members need to be instantiated to allocate memory, so static members cannot access non-static members .. because static members exist in the memory, non-static members can directly access static members in the class.

1. static global variables

Definition: before a global variable, add the keyword "static". the variable is defined as a static global variable.

Features:
A. The variable allocates memory in the global data zone.
B. initialization: if explicit initialization is not performed, the value 0 is implicitly initialized (automatic variables are random unless explicitly initialized ).
C,The access variable is only visible in this source file. Strictly speaking, it should start from the definition to the end of this file.

2. static local variables

Features:
A. The variable allocates memory in the global data zone.
B. initialization: if explicit initialization is not performed, the value 0 will be implicitly initialized, and subsequent function calls will not be initialized.
C,It always resides in the global data zone until the program running ends. But its scope is local scope. when the function or statement block defining it ends, its scope ends.

Static data members follow the public, protected, and private access rules like common data members;

Because static data members allocate memory in the global data zone and all objects in this class are shared, they do not belong to a specific class object and are visible in the scope when no class object is generated, that is, when no class is generated, we can operate on it; ''static data member initialization is different from general data member initialization. The static data member initialization format is:

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

Static data members of a class can be accessed in two forms:
<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 public member), you can reference the static data member in the program in the above format;

Static data members are mainly used when each object has the same attribute. For example, for a deposit type, the interest of each instance is the same. Therefore, the interest should be set as the static data member of the deposit class. There are two advantages: first, no matter how many deposit-type objects are defined, interest data members share the memory allocated to the global data area, thus saving storage space. Second, once the interest needs to be changed, the interest of all deposit objects will be changed once;

For more articles about PHP static member variables, refer to PHP!

Related Article

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.