Home > Backend Development > PHP Tutorial > The role of static keyword in php

The role of static keyword in php

黄舟
Release: 2023-03-12 11:56:01
Original
8816 people have browsed it

Summary of the following article:

1. For static variables: localization (name conflict), initialization = 0, unique sharing (static area) . In particular, for class static member variables: (1) Belong to the entire class and can be accessed directly through the class name instead of through the instance (2) Must be initialized, declared static within the class, initialized outside the class (static cannot be added)

2. For class static member functions, (1) there is no this pointer, only static member variables and static member functions can be accessed, and cannot be declared as virtual functions (2) Often used in subclasses in multi-threads.

-------------------------------------------------- -------------------------------------------------- -------------------------------------------------- ---

1. What is static?
static is a very commonly used modifier in C++. It is used to control the storage method and visibility of variables.

2. Why introduce static?
When the program executes to its definition point, the compiler allocates space for it on the stack. As we all know, functions are allocated on the stack. The space will be released at the end of the execution of this function, which raises a question: If you want to save the value of this variable in the function to the next call, how to achieve it? The easiest way to think of is to define a global variable, but defining it as a global variable has many disadvantages. The most obvious disadvantage is that it destroys the access scope of this variable (making the variables defined in this function not only controlled by this function). ).

3. When to use static?
You need a data object to serve the entire class rather than a certain object, and at the same time try not to destroy the encapsulation of the class, that is, you require this member to be hidden inside the class. Not visible to the outside world.

4. The internal mechanism of static:
Static data members must exist when the program starts running. Because functions are called while the program is running, static data members cannot be allocated and initialized within any function.
In this way, there are three possible places for its space allocation. One is the header file as the external interface of the class, where there is the class declaration; the second is the internal implementation of the class definition, where there are the member function definitions of the class; the third is the application The global data declaration and definition before the main() function of the program.
Static data members need to actually allocate space, so they cannot be defined in the declaration of the class (only data members can be declared). The class declaration only declares the "size and specifications" of a class and does not perform actual memory allocation, so it is wrong to write a definition in the class declaration. It also cannot be defined outside a class declaration in a header file, since that would cause it to be redefined in multiple source files that use the class.
Static is introduced to tell the compiler to store variables in the static storage area of ​​the program instead of the space on the stack. Static
Data members are initialized in the order in which they are defined. Note that when nesting static members, ensure The nested member has been initialized. The order of elimination is the reverse order of initialization.

5. Advantages of static:
It can save memory because it is common to all objects. Therefore, for multiple objects, static data members are only stored in one place for all objects to share. The value of a static data member is the same for every object, but its value can be updated. As long as the value of the static data member is updated once, all objects are guaranteed to access the same updated value, which can improve time efficiency.

6. When referencing static data members, use the following format:
    <Class name>::<Static member name>
If the access rights of the static data members are allowed (that is, public members), the static data members can be referenced in the program according to the above format.

7. Notes:
(1)The static member function of the class belongs to the entire class rather than the object of the class, so it does not have this pointer, which results in It can only access static data and static member functions of the class.
(2) Static member functions cannot be defined as virtual functions.
(3) Since static members are declared in the class and operate outside it, the operation of taking their address is somewhat special. The variable address is a pointer to its data type, and the function address type Is a "nonmemberfunction pointer".

(4) Since the static member function does not have this pointer, it is almost equivalent to the nonmember function. The result is an unexpected benefit: it becomes a callback function, which allows us to combine C++ and C-based X Integrated with the Window system, it has also been successfully applied to thread functions.
(5)static does not increase the time and space overhead of the program. On the contrary, it also shortens the access time of the subclass to the static members of the parent class and saves the memory space of the subclass.
                                                                                   (6) Static data members are preceded by the keyword static when .
(7)Static data members are stored statically, so they must be initialized.
​ (8) Static member initialization is different from general data member initialization:
​ ​ Initialization is performed outside the class, without static in front, so as not to be confused with general static variables or objects;
Initialization does not add the member's access permissions control symbol, PRIVATE, PUBLIC; Format of member initialization:
                                                                                                                                               ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, Define a static variable in the subclass that is the same as the parent class to shield the influence of the parent class. There is one thing to note here: we say that static members are shared by parent classes and subclasses, but we have repeatedly defined static members. Will this cause an error? No, our compiler uses a clever trick: name-mangling to generate unique identifiers.

Static data members

In a class, static members can realize data sharing between multiple objects, and using static data members will not destroy the hidden principle, that is, guarantee security. Therefore, static members are members shared among all objects of the class, not members of a certain object.

Using static data members can save memory, because it is common to all objects. Therefore, for multiple objects, static data members are only stored in one place for all objects to share. The value of a static data member is the same for every object, but its value can be updated. As long as the value of the static data member is updated once, all objects are guaranteed to access the same updated value, which can improve time efficiency.

The usage methods and precautions for static data members are as follows:

1. Add the keyword static before the definition or description of static data members.

 2. Static member initialization is different from general data member initialization. The format of static data member initialization is as follows: ::=

This indicates :

​ ​ (1) Initialization is performed outside the class, without static in front, so as not to be confused with general static variables or objects.

 (2) Do not add the member’s access control characters private, public, etc. during initialization.

 (3) Use the scope operator during initialization to indicate the class to which it belongs. Therefore, static data members are members of the class, not the object.

 3. Static data members are stored statically. They have a static lifetime and must be initialized.

 4. When referencing static data members, use the following format:

 ::

If the static data member If the access permission is allowed (that is, public members), the static data members can be referenced in the program according to the above format.

Static member function


Static member functions are the same as static data members. They are all static members of the class and they are not object members. Therefore, references to static members do not need to use object names.
In the implementation of static member functions, non-static members described in the class cannot be directly referenced, but static members described in the class can be referenced. If a non-static member needs to be referenced in a static member function, it can be referenced through an object.

Let’s look at an example:


#
include
 <iostream.h>
class Point
{
public:
void output()
{
}
static void init()
{  
} 
};
void main( void )
{
Point pt;
pt.init();
pt.output(); 
}
Copy after login

There will be no errors in compiling like this.

Look like this


#include <iostream.h>
class Point
{
public:
void output()
{  
}
static void init()
{  
} 
};
void main( void )
{
Point::output();
}
Copy after login

This will cause an error when compiling,

Error message

: illegal call of non-static member function, why? Because when a specific object of a class is not instantiated, the class is not allocated memory space. Okay, look at the following example:

#include <iostream.h>
class Point
{
public:
void output()
{  
}
static void init()
{  
} 
};
void main( void )
{
Point::init();
}
Copy after login

There will be no compilation errors at this time, because when the class is defined, its static data and member functions have its memory. Area, it does not belong to any specific object of the class.

Okay, look at the following example:


#include <iostream.h>
class Point
{
public:
void output()
{  
}
static void init()
{ 
   x = 0;
   y = 0;
}
private:
int x;
int y;
};
void main( void )
{
Point::init();
}
Copy after login

Compilation error:


illegal reference to data member 'Point::x' in a static member functionillegal
reference to data
member 'Point::y' in a static member functionIn a static member function The data member was incorrectly referenced. It’s still the same problem. Static members (functions) do not belong to any specific object, so there is already a memory area before the specific object of the class is declared.
But now it is not static The data member has not allocated memory space, so the call here is wrong, just like using a variable in advance without declaring it.
That is to say, non-static member variables cannot be referenced in static member functions.
Okay, let’s look at the following example:

#include <iostream.h>class Point{public:void output(){   x = 0;   y = 0;   init();  }static void init(){
}private:int x;int y;};void main( void ){Point::init();}
Copy after login

好的,这样就不会有任何错误。这最终还是一个内存模型的问题,
任何变量在内存中有了自己的空间后,在其他地方才能被调用,否则就会出错。
好的再看看下面的例子:

#include <iostream.h>
class Point
{
public:
void output()
{ 
}
static void init()
{ 
   x = 0;
   y = 0;
}
private:
static int x;
static int y;
};
void main( void )
{
Point::init();
}
Copy after login

编译:
Linking...
test.obj : error LNK2001: unresolved external symbol "private: static int Point::y"
test.obj : error LNK2001: unresolved external symbol "private: static int Point::x"
Debug/Test.exe : fatal error LNK1120: 2 unresolved externals
执行 link.exe 时出错.
可以看到编译没有错误,连接错误,这又是为什么呢?
这是因为静态的成员变量要进行初始化,可以这样:

#include <iostream.h>
class Point
{
public:
void output()
{ 
}
static void init()
{ 
   x = 0;
   y = 0;
}
private:
static int x;
static int y;
};
int Point::x = 0;
int Point::y = 0;
void main( void )
{
Point::init();
}
Copy after login

在静态成员数据变量初始化之后就不会出现编译错误了。
再看看下面的代码:

#include <iostream.h>
class Point
{
public:
void output()
{ 
}
static void init()
{ 
   x = 0;
   y = 0;
}
private:
static int x;
static int y;
};
void main( void )
{
}
Copy after login

编译没有错误,为什么?
即使他们没有初始化,因为我们没有访问x,y,所以编译不会出错。

C++会区分两种类型的成员函数:静态成员函数和非静态成员函数。这两者之间的一个重大区别是,静态成员函数不接受隐含的this自变量。所以,它就无法访问自己类的非静态成员。

在某些条件下,比如说在使用诸如pthread(它不支持类)此类的多线程库时,就必须使用静态的成员函数,因为其地址同C语言函数的地址兼容。这种铜限制就迫使程序员要利用各种解决办法才能够从静态成员函数访问到非静态数据成员。

第一个解决办法是声明类的所有数据成员都是静态的。运用这种方式的话,静态的成员函数就能够直接地访问它们,例如:

class Singleton
{
public:
   static Singleton * instance();
private:
   Singleton * p;
   static Lock lock;
};
Singleton * Singleton::instance()
{
lock.getlock(); 
// fine, lock is static
if (!p)
   p=new Singleton;
lock.unlock();
return p;
}
Copy after login


这种解决方法不适用于需要使用非静态数据成员的类。

访问非静态数据成员

将参照传递给需要考量的对象能够让静态的成员函数访问到对象的非静态数据:

class A
{
public:
   static void func(A & obj);
   intgetval() const; 
//
non-static
 
member
 
function
private:
intval;
};
Copy after login


静态成员函数func()会使用参照obj来访问非静态成员val

voidA::func(A & obj)
{
   int n = obj.getval();
}
Copy after login


将一个参照或者指针作为静态成员函数的自变量传递,就是在模仿自动传递非静态成员函数里this自变量这一行为。

The above is the detailed content of The role of static keyword in php. For more information, please follow other related articles on the PHP Chinese website!

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