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

The role of static keyword in php

Jul 02, 2017 am 11:54 AM
php static Keywords

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

See all articles