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 are the differences between php static methods and instantiated methods

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

Share

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

This article mainly introduces the relevant knowledge of "what is the difference between php static method and instantiation method". The editor shows you the operation process through an actual case, the operation method is simple and fast, and it is practical. I hope this article "what is the difference between php static method and instantiation method" can help you solve the problem.

Differences: 1, static methods can be directly called without opening up space and other operations, while instance methods need to open up space operations; 2, static methods share a space and the same data, while instance methods do not share the same space and data; 3, static methods do not support chain writing, while instance methods support chain writing.

This article operating environment: Windows10 system, PHP7.1 version, Dell G3 computer.

What is the difference between php static method and instantiated method

Static methods have only one copy in memory and are created when the code is loaded, and instantiated methods or classes are created only when new. Each instantiated object represents a different instance, while there is only one static. Pay special attention to the fact that static methods share resources in multithreading.

Difference:

1. Static method calls do not need new, class name:: method name.

Such as:

User::find ()

Note: non-static properties cannot be called in static methods.

The instance method requires new.

Such as:

12$ userObj = new User;$userObj- > find ()

2. There is only one static method in memory, and resources are shared within an php life cycle.

Note: static methods and properties load as the class loads, so too many static methods consume more memory.

Each new of the instance method opens up a separate space, that is, there will be multiple copies in memory.

3. Static method performance is called directly, which does not need to open up space and other operations, and is better in terms of time and efficiency.

The instance method needs some time to open up space and other operations.

4. Static methods share the same space and the same data, and static methods are more suitable for all scenarios.

Sample method multiple instances do not share the same space and data

5. Static method chain writing is not supported.

Instance method support. Such as:

1$ userObj- > fields ('uid')-> where (' uid > 0')-> find (); that's all for "what's the difference between php static methods and instantiated methods". 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