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

Static variable (static)

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

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

I. static variables

In the above example, the local variable $an in the function is declared as a static variable using the static keyword, with an initial value of 0. 0. When the function is executed for the first time, the static variable $a changes from the initial value of 0 to 1. When the function is executed for the first time, the static variable $an is not released, but the result is saved in static memory. On the second execution, $a gets the result of the previous calculation from memory, continues the calculation, and stores the result 2 in the static memory space. Each time the function is executed in the future, the static variable will get the previous storage result from its own static memory space and calculate it as the initial value.

II. The use of the static keyword

Static members in a class are accessed directly using the class name instead of an object, in the following format:

Class name: static member property name; / / static member attribute can be accessed in this way both outside the class and in member methods

Class name: static member method (); / / static member method can be accessed in this way both outside the class and in member methods

In member methods declared in the class, you can also use the keyword "self" to access other static members. Because a static member belongs to a class, not to any object, it cannot be referenced with $this, and the self keyword provided to us in PHP is the keyword used to represent the class in the class's member methods. The format is as follows:

Self:: static member property name; / / use this way to access static member properties in this class in the member methods of the class

Self:: static member method name (); / / access static member methods in this class in this way in the member methods of the class

The result of the code running is 3 / 3

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

Network Security

Wechat

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

12
Report