Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use static properties and static methods in php

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the relevant knowledge of "static attributes and static methods in php". The editor shows you the operation process through actual cases. The operation method is simple, fast and practical. I hope this article "how to use static attributes and static methods in php" can help you solve the problem.

In php, member properties and methods modified by the static keyword are called static properties and static methods, and access to the syntax of static properties and static methods "class name:: $static property" and "class name:: static method ()".

Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

In PHP, the member attributes and methods modified by the static keyword are called static properties and static methods. Here, they can be called static members. The static members in the class are different from the general members in the class. Static members are not instantiated into the object, that is to say, we do not need to instantiate a class to access static members through the class.

It's easy to declare static variables in a class, and you can make the ordinary member of the class a static member by adding a static keyword in front of it. In this way, we can access these static members of the class directly without instantiating the class. The syntax format for accessing static members is as follows:

Class name:: $static property class name:: static method ()

Where the:: symbol is called the scope resolution operator and is used to access static members, static methods, and constants, and can also be used to override members and methods in a class.

If you want to access static properties in member methods within the class, simply precede the name of the static property with the operator self::.

The following is a simple example to demonstrate the definition of a static member:

The running results are as follows:

Note: in static methods, only static variables can be called, not ordinary variables, while ordinary methods can call static variables.

With static members, except that there is no need to instantiate the object, another function is to save the modified static data after the object is destroyed so that it can be used again next time, which is similar to the static variables we described earlier.

[example] declare a static variable $num, then declare a method, call the static variable inside the method and add 1 to the value of the variable; then instantiate the object in the class, and finally, call the method in the class.

The running results are as follows:

Static members do not need to instantiate objects, and memory space is allocated when the class is first loaded, so it is faster to call static members directly. However, if the static members declare too much, the space is occupied all the time, which affects the running speed of the system.

Description:

Subclasses can override the static methods of the parent class.

The:: symbol is needed to access static variables within the method. Cannot use $this

Both static methods and static variables are inherited by subclasses.

Static variables cannot be accessed by->, but by:: (double colon)

Objects can call static member methods directly through the object name->, which is the same as calling ordinary methods.

This is the end of the content on "how to use static properties and methods in php". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report