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
WBOYOriginal
2016-07-13 09:57:25814browse

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 ;
cout9b1b219827a5e03a6aeed607e505996cfn(). But compared with ordinary functions, static member functions do not have this pointers 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();/声明静态成员函数
private:
 int a,b,c;
 static int Sum;//声明静态数据成员
};
int Myclass::Sum=0;//定义并初始化静态数据成员

Myclass::Myclass(int a,int b,int c)
{
 this->a=a;
 this->b=b;
 this->c=c;
 Sum+=a+b+c; //非静态成员函数可以访问静态数据成员
}

void Myclass::GetSum() //静态成员函数的实现
{
// cout<<a<<endl; //错误代码,a是非静态数据成员
 cout<<"Sum="<<Sum<<endl;
}

void main()
{
 Myclass M(1,2,3);
 M.GetSum();
 Myclass N(4,5,6);
 N.GetSum();
 Myclass::GetSum();
}

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...
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