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

What is php static method

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

Share

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

This article introduces the knowledge of "what is php static method". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

In php, member methods modified by the static keyword are called static methods. The memory space of static methods is fixed, only static members in the class can be accessed, and static methods can be accessed through the "class name:: static method ()" statement.

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

Static method

The memory space of static methods is fixed, which is relatively resource-efficient.

To create an instance, it is necessary to open up a new memory. The resource-consuming static method belongs to the class and can be used before the class is instantiated.

Static methods can only access static members of a class

Only static variables and other static methods can appear inside the static! And keywords such as this cannot be used in the static method, because it belongs to the entire class

Static methods and static variables always use the same block of memory after they are created, while the use of instances creates multiple memories.

Advantages of static methods:

(1) it can be used anywhere in the code (assuming the class can be accessed)

(2) each instance of the class can access the static property defined in the class, and the static property can be used to set the value, which can be used by all objects of the class.

(3) static properties or methods can be accessed without an instance object.

The difference between static method and ordinary method

Static methods can be used before the object is created, and non-static methods must be called through objects that come out of new.

Static methods can be called directly through the class name:: method name. The ordinary method needs to create an instance, that is, new an object, and then call the static class through the object name-> method name, which can only contain static members, otherwise a compilation error will be thrown

A non-static class can contain both non-static members and static members. A static class cannot be instantiated because the static class causes the C # compiler to mark the class as both abstract and sealed, and the compiler does not generate an instance constructor in the type, so that the static class cannot be instantiated.

Non-static classes can, and access to static members can only be accessed through the class, because static members belong to the class.

This is the end of the content of "what is php static method". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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