Home  >  Article  >  Backend Development  >  Detailed explanation of PHP static member variables

Detailed explanation of PHP static member variables

墨辰丷
墨辰丷Original
2018-05-25 14:48:401395browse

This article mainly introduces the detailed explanation of PHP static member variables. Interested friends can refer to it. I hope it will be helpful to everyone.

Static members: Members in a static class are static members by adding the static modifier. You can directly use the class name static member name to access this static member, because static members exist 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 variables

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. The access variable is only visible in the source file. Strictly speaking, it should start from the place of definition and end in this file.

2. Static local variables

Features:

A. This variable is in global data Allocate memory.

B. Initialization: If not explicitly initialized, it will be implicitly initialized to 0, and subsequent function calls will no longer be initialized.

C. It always resides in the global data area until the end of the program. But its scope is local scope. When the function or statement block that defines it ends, its scope ends.

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, so, It does not belong to a specific class object, and its 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:

Data type><Class name>::<Static data member name>=<Value>

Static data members of a class have two access forms:

. or ::

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 certain attribute. 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; helped.

Related recommendations:


Constants and

variables in php

Detailed explanations with pictures and texts

Angularjs How to set global
variables

(graphic tutorial)

In-depth understanding of block-level scope and privateness in JavaScript
Variables

and module mode (graphic tutorial)


The above is the detailed content of Detailed explanation of PHP static member variables. For more information, please follow other related articles on the PHP Chinese website!

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