Home  >  Article  >  Backend Development  >  PHP static member variables

PHP static member variables

高洛峰
高洛峰Original
2017-02-18 16:39:271060browse

Static member: A member in a static class is a static member by adding the static modifier. You can directly use the class name+static membername to access this static member, because static members Exists in memory, non-static members need to be instantiated before memory is allocated, so static members cannot access non-static members. Because static members exist in memory, non-static members can directly access static members in the class.

1. Static global variable

Definition: Add the keyword static before the global variable and the variable is defined as a static global variable.

Features:
A. This variable allocates memory in the global data area.
 B. Initialization: If not explicitly initialized, it will be implicitly initialized to 0 (automatic variables are random unless explicitly initialized).
 C, Access variables are only visible in the original source file. Strictly speaking, they should start from the point of definition and end in this file.

2. Static local variables

<span style="font-size: 15px">特点:</span> <br><span style="font-size: 15px">  A、该变量在全局数据区分配内存。 </span><br><span style="font-size: 15px">  B、初始化:如果不显式初始化,那么将被隐式初始化为0,以后的函数调用不再进行初始化。 </span><br><span style="font-size: 15px">  C、<strong>它始终驻留在全局数据区,直到程序运行结束。但其作用域为局部作用域,当定义它的函数或 语句块结束时,其作用域随之结束。<br></strong></span>

Static data members follow the same public, protected, and private access rules as ordinary data members;

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

                                                                                                                                                               . #Static data members of a class have two access forms:                                                                                                                                                                                                                                                                                        If the access rights of static data members are allowed (that is, public members), you can reference static data members in the program according to the above format; Static data members are mainly used when each object has the same item attributes. For example, for a deposit class, the interest rate for each instance is the same. Therefore, interest should be set as a static data member of the deposit class. This has two advantages. First, no matter how many deposit class objects are defined, the interest data members all share the memory allocated in the global data area, so storage space is saved. Second, once the interest needs to be changed, as long as it is changed once, the interest of all deposit objects will be changed;

For more PHP static member variables, please pay attention to the PHP Chinese website for related articles!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn