PHP static member variables and non-static member variables, php static
Source: Internet
Author: User
Data members can be divided into static variables and non-static variables.
Static members: static members are added to static class members, that is, static members. You can directly access this static member using the class name + static member name, because static members exist in memory, non-static members need to be instantiated to 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 members without Static are non-static members. After the class is instantiated, it can be accessed by the instantiated class name. The lifetime of non-static members depends on the lifetime of the class. The concept of lifetime does not exist for static members, because static members always reside in the content ...
A class can also contain static and non-static members, and the class also includes static constructors and non-static constructors ...
Summarized in two aspects, the first aspect is mainly relative to process-oriented, that is, no class is involved in this aspect, and the second aspect is mainly to explain the role of static in the class relative to object-oriented.
First, the static keyword in process-oriented design
1.Static global variables
Definition: Before the global variable, add the keyword static and the variable is defined as a static global variable.
Features:
A. This variable allocates memory in the global data area.
B. Initialization: If it is not explicitly initialized, it will be implicitly initialized to 0 (auto variables are random, unless explicitly initialized)
C. Access variables are only visible in this source file. Strictly speaking, it should start at the definition and end at the end of this file.
Example (taken from C ++ programming tutorial --- Qian Neng editor P103): //file1.cpp
// Example 1
#include <iostream.h>
void fn ();
static int n; // Define a static global variable
void main ()
{
n = 20;
cout << n << endl;
fn ();
}
void fn ()
{
n ++;
cout << n << endl;
}
D. The const constant declared under the file scope defaults to the static storage type.
Static variables are allocated memory in the global data area, including static local variables to be mentioned later. For a complete program, the distribution in memory is as follows:
Code area
Global data area
Dump area
Stack area
The dynamic data generated by new in general programs 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 static local variables inside the function) are stored in the global data area. The data in the global data area does not free up space because the function exits. Attentive readers may find that the code in Example 1 will
static int n; // Define a static global variable
To:
int n; // define global variables
The program runs normally. It is true that defining global variables can share variables in files, but defining static global variables also has the following benefits:
Static global variables cannot be used by other files; (seems to be different from extern)
Variables with the same name can be defined in other files without conflict;
You can change the above example code to the following:
// Example 2
// File1
#include <iostream.h>
void fn ();
static int n; // Define a static global variable (can only be used in this file)
void main ()
{
n = 20;
cout << n << endl;
fn ();
}
// File2
#include <iostream.h>
extern int n; (this variable can be referenced in other files)
void fn ()
{
n ++;
cout << n << endl;
}
Compile and run Example 2 and you will find that the above code can be compiled separately, but an error occurs when linking. Try to
static int n; // Define a static global variable
Change to
int n; // define global variables
Compile and run the program again, and carefully understand the difference between global variables and static global variables.
2.Static local variables
Definition: When you add the static keyword before a local variable, you define a static local variable.
Let's start with an example of a static local variable, as follows:
// Example 3
#include <iostream.h>
void fn ();
void main ()
{
fn ();
fn ();
fn ();
}
void fn ()
{
static n = 10;
cout << n << endl;
n ++;
}
Generally, a variable is defined in the function body, and the stack variable is allocated to the local variable whenever the program runs to the statement. But as the program exits the function body, the system will reclaim stack memory and local variables will be invalidated accordingly.
But sometimes we need to save the value of a variable between two calls. The usual idea is to define a global variable to achieve this. But in this way, the variable no longer belongs to the function itself, and is no longer controlled by the function alone, which brings inconvenience to the maintenance of the program.
Static local variables can just solve this problem. Static local variables are stored in the global data area, rather than in the stack, and each value is held until the next call until a new value is assigned next time.
Features:
A. This variable allocates memory in the global data area.
B. Initialization: If it is not explicitly initialized, it will be implicitly initialized to 0, and subsequent function calls will not be initialized.
C. It always resides in the global data area until the end of program operation. However, its scope is local, and its scope ends when the function or statement block that defines it ends.
3. Static functions (note the difference from static member functions of classes)
Definition: Add the static keyword before the return type of a function, and the function is defined as a static function.
Features:
A. A static function is different from an ordinary function. It is only visible in the file in which it is declared, and cannot be used by other files.
Examples of static functions:
// Example 4
#include <iostream.h>
static void fn (); // Declares a static function
void main ()
{
fn ();
}
void fn () // Define a static function
{
int n = 10;
cout << n << endl;
}
Benefits of defining static functions:
Static functions cannot be used by other files;
Functions with the same name can be defined in other files without conflicts;
Second, the object-oriented static keywords (static keywords in the class)
1.Static data members
Add the keyword static before the declaration of the data member in the class, and the data member is the static data member in the class. Let's start with an example of a static data member.
// Example 5
#include <iostream.h>
class Myclass
{
public:
Myclass (int a, int b, int c);
void GetSum ();
private:
int a, b, c;
static int Sum; // Declaration of static data members
};
int Myclass :: Sum = 0; // Define and initialize static data members
Myclass :: Myclass (int a, int b, int c)
{
this-> a = a;
this-> b = b;
this-> c = c;
Sum + = a + b + c;
}
void main ()
{
Myclass M (1,2,3);
M.GetSum ();
Myclass N (4,5,6);
N.GetSum ();
M.GetSum ();
}
As can be seen, static data members have the following characteristics:
For non-static data members, each class object has its own copy. Static data members are treated as members of the class. No matter how many objects of this class are defined, there is only one copy of the static data member in the program, which is shared by all objects of this type. That is, static data members are common to all objects of the class. For multiple objects of this class, static data members are allocated memory only once and are shared by all objects. Therefore, the value of the static data member is the same for each object, and its value can be updated;
Static data members are stored in the global data area. Space is allocated when a static data member is defined, so it cannot be defined in a class declaration. In Example 5, the statement int Myclass :: Sum = 0; is a static data member definition;
Static data members follow the public, protected, and private access rules just like ordinary data members;
Because static data members allocate memory in the global data area, all objects belonging to this class are shared, 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 normal data member initialization. The format of static data member initialization is:
<Data type> <class name> :: <static data member name> = <value>
There are two forms of access to static data members of a class:
<Class object name>. <Static data member name> or <class type name> :: <static data member name>
If the access rights of static data members allow (that is, members of public), the static data members can be referenced 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 class, the interest on each instance is the same. Therefore, interest should be set as a static data member of the deposit class. This has two benefits. First, no matter how many deposit class objects are defined, the interest data members share the memory allocated in the global data area, so saving storage space. Secondly, once the interest needs to be changed, as long as it is changed once, the interest of all deposit objects will be changed;
There are two advantages to using static data members over global variables:
2, static member functions
Like static data members, we can also create a static member function that serves the entire class instead of the specific objects of a class. Static member functions, like static data members, are internal implementations of a class and are part of the class definition. Ordinary member functions generally imply a this pointer, this pointer points to the object of the class itself, because ordinary member functions always concretely belong to a specific object of a certain class. Normally, this is the default. For example, the function fn () is actually this-> fn (). But compared with ordinary functions, static member functions do not have a this pointer because they are not associated with any object. In this sense, it cannot access non-static data members belonging to the class object, nor can it access non-static member functions, it can only call the remaining static member functions. Here is an example of a static member function.
// Example 6
#include <iostream.h>
class Myclass
{
public:
Myclass (int a, int b, int c);
static void GetSum (); / Declares a static member function
private:
int a, b, c;
static int Sum; // Declaration of static data members
};
int Myclass :: Sum = 0; // Define and initialize static data members
Myclass :: Myclass (int a, int b, int c)
{
this-> a = a;
this-> b = b;
this-> c = c;
Sum + = a + b + c; // Non-static member functions can access static data members
}
void Myclass :: GetSum () // The implementation of static member functions
{
// cout << a << endl; // Error code, a is a non-static data member
cout << "Sum =" << Sum << endl;
}
void main ()
{
Myclass M (1,2,3);
M.GetSum ();
Myclass N (4,5,6);
N.GetSum ();
Myclass :: GetSum ();
}
The static member functions can be summarized as follows:
Function definitions that appear 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 State member functions and static data members;
Static member functions cannot access non-static member functions and non-static data members;
Because there is no extra overhead of this pointer, static member functions have a slight increase in speed compared to the 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 are not related to an instance of an object, but only to the class itself. They are used to implement the functions and data to be encapsulated by the class, but do not include the functions and data of specific objects. Static members include static methods and static properties.
Static properties contain the data to be encapsulated in the class and can be shared by all instances of the class. In fact, except that it belongs to a fixed class and restricts access, the static properties of the class are very similar to the global variables of a function.
Static methods implement the functions that the class needs to encapsulate, and have nothing to do with specific objects. Static methods are very similar to global functions. Static methods can fully access the properties of the class, or they can be accessed by the 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, and 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 Do not create this variables inside static methods, even if you call them from an object.
You can write a method to tell if this is established to show whether it is called statically or non-statically. Of course, if you use the static keyword, this method is always 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 properties of the objects that instantiate the class.
The efficiency of 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 method of a class. It is more reasonable to use a static method, otherwise there will be additional overhead due to new.
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.