Home > Backend Development > PHP Tutorial > PHP static member variables and non-static member variables, php static_PHP tutorial

PHP static member variables and non-static member variables, php static_PHP tutorial

WBOY
Release: 2016-07-13 09:57:25
Original
826 people have browsed it

PHP static member variables and non-static member variables, php static

Data members can be divided into two types: static variables and non-static variables .
Static members: A member in a static class is a static member by adding the static modifier. You can directly use the class name or static member name to access this static member. Because static members exist in memory, non-static members need to be instantiated. Allocate memory, 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.

Non-static members: all those that are not static Members are non-static members. When a class is instantiated, it can be accessed through the instantiated class name. The lifetime of non-static members is determined by the lifetime of the class. Static members do not have the concept of lifetime. Because static members always reside in the content..

A class can also contain static members and non-static members, and the class also includes static constructors and non-static constructors..

Let’s summarize it in two aspects. The first aspect is mainly relative to process orientation, that is, classes are not involved in this aspect. The second aspect is relative to object orientation, mainly explaining the role of static in classes.

1. The static keyword in process-oriented design

1. Static global variable

definition: add the key before the global variable With the word static, 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 definition point and end in this file. ​​​​​🎜> void fn();
static int n; //Define static global variable
void main()
{
                                                                                                                                                > fn();
}

void fn()
{
n ;
cout< }

D. Const constants declared in file scope default to static storage type.

Static variables allocate memory in the global data area, including static local variables that will be mentioned later. For a complete program, the distribution in memory is as follows:


Generally, the dynamic data generated by new in the program is stored in the heap area, and the automatic variables inside the function are stored in the stack area. Automatic variables generally release space when the function exits, and static data (even if the function Internal static local variables) are also stored in the global data area. The data in the global data area will not release space when the function exits. Careful readers may find that the code in Example 1 contains

                       static int n; //Define static global variables

changed to:

1c3c1e864dc7d721f1b69d22730270c7

Regarding static member functions, it can be summarized as the following points:

  • The function definition that appears outside the class cannot specify the keyword static;
  • Static members can access each other, including static member functions accessing static data members and accessing static member functions;
  • Non-static member functions can access static member functions and static data members arbitrarily;
  • Static member functions cannot access non-static member functions and non-static data members;
  • Since there is no additional overhead of this pointer, static member functions will have a slight increase in speed compared to global functions of the class;
  • To call a static member function, you can use the member access operators (.) and (->) to call a static member function for a class object or a pointer to a class object;

    Static members of a class are different from general class members: static members have nothing to do with the instance of the object, only the class itself. They are used to implement the functions and data that the class wants to encapsulate, but do not include the functions and data of specific objects. Static members include static methods and static properties.

    Static properties contain data to be encapsulated in a class and can be shared by all instances of the class. In fact, in addition to belonging to a fixed class and restricting access methods, the static properties of the class are very similar to the global variables of the function.

    Static methods implement functions that need to be encapsulated by the class and have nothing to do with specific objects. Static methods are very similar to global functions. Static methods can fully access the attributes of the class, or Accessed by an instance of the object, regardless of the access qualifier.

    A class that does not contain any non-static members can be called a static class. A static class can also be understood as a namespace for global variables and functions!

    Ordinary methods are called with ->. PHP will create a this variable. Static methods do not belong to any object. In some cases, we even want to call it when there is no valid object, then we should use static Methods. PHP will not create this variable inside static methods, even if you call them from an object.

    You can write a method to show whether it is called statically or non-statically by checking whether this is established. Of course, if you use the static keyword, this method will always be static no matter how it is called.

    Your class can also define constant properties. You don’t need to use public static, just use the const keyword. Constant properties are always static. They are properties of the class, not the properties of the object that instantiates the class. .

    Efficiency issues between PHP static methods and non-static methods

    1. The access efficiency of static members is not necessarily higher than that of non-static members;
    2. You only need to call the return value of a class method, and it is more reasonable to use static methods, otherwise there will be additional overhead due to new.

    www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/983243.htmlTechArticlePHP static member variables and non-static member variables. PHP static data members can be divided into static variables and non-static variables. . Static members: Add the static modifier to the members in the static class, that is...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template