Does using static methods and properties in PHP us

2020-06-30 05:09发布

I'm working on a web application that sees dozens of concurrent users per second. I have a class that will be instantiated many times within the same page load. In that class, I have some properties that will always be the same across every object, so I'm thinking about declaring these properties as static in an effort to reduce the memory that will be used when multiple instances of this class are instantiated during the same page request.

Will doing this use less memory for this application because PHP can store the value of the static properties only once? Will doing this save memory across concurrent users, or just within each PHP process?

How does this work for methods? If this means objects can recycle the same methods, then why wouldn't all methods of a class be declared static if you are trying to save on memory?

I don't feel entirely comfortable with why and when one would declare a property or method static, but I do understand that declaring them as static allows them to be accessed without instantiating an object of the class ( this feels like a hack ... these methods and properties should be somewhere else ... no? ). I'm specifically interested in the way a static declaration affects memory usage in an effort to keep memory usage as low as possible on my web server ... and in general so I have a better understanding of what is going on.

8条回答
闹够了就滚
2楼-- · 2020-06-30 05:43

I'm not an expert in memory management of PHP, but I would say you DON'T save much. If and how much you save depends on some aspects:

  • The size of the object (when you create an instance, how much other data does the object hold).
  • The number of objects you create.

Especially the number of objects is important. If you have only one instance, you save 50%. In this case:

Case A - Static: You don't instantiate the object, but just use the class definition, which is stored in the memory. But the class definition is loaded for every REQUEST, which means you have the same amount of class definitions in the memory as you have concurrent requests.

Case B - Instances: Additionally to case A you also have an instance of this object for each request, thus double the memory usage for this part of you software.

Finally: if it is easier for you to work with static parameters instead of instantiating the class every time, you should go with the static way. But don't expect too much of a memory boost.

查看更多
戒情不戒烟
3楼-- · 2020-06-30 05:45

Look static vs singleton tests: http://moisadoru.wordpress.com/2010/03/02/static-call-versus-singleton-call-in-php/

Note: for some reasons, stackoverflow didn't show multilne topic, so i'm adding a picture.

Number of runs  Singleton call time (s)     Static call time (s)
100             0.004005                    0.001511
1,000           0.018872                    0.014552
10,000          0.174744                    0.141820
100,000         1.643465                    1.431564
200,000         3.277334                    2.812432
300,000         5.079388                    4.419048
500,000         8.086555                    6.841494
1,000,000       16.189018                   13.696728

Checkout more details here: https://stackoverflow.com/a/3419411/260080

查看更多
劳资没心,怎么记你
4楼-- · 2020-06-30 05:45

Static method calls are faster over many iterations, but static methods don't really save memory.

If the class you are declaring doesn't have any properties that need to be unique to each object instance, then you could declare every method and property as static. If, however, you have properties that need to be bound to each object, then static methods are not helpful. The reason is because inside static methods, there is no reference to $this so you cannot reference object properties from static methods.

Read up on the Static Keyword for a better understanding of this.

查看更多
可以哭但决不认输i
5楼-- · 2020-06-30 05:48

If you share the data use a static. It's faster and saves you the process of object instantiation. Singletons win versus statics when you need a single entry point. I covered this on my blog about 1 week ago.

查看更多
The star\"
6楼-- · 2020-06-30 05:53

When you declare a class method/variable as static, it is bound to and shared by the class, not the object. From a memory management perspective what this means is that when the class definition is loaded into the heap memory, these static objects are created there. When the class's actual object is created in the stack memory and when updates on the static properties are done, the pointer to the heap which contains the static object gets updated. This does help to reduce memory but not by much.

From a programming paradigm, people usually choose to use static variables for architectural advantages more than memory management optimization. In other words, one might create static variables like you mentioned, when one wants to implement a singleton or factory pattern. It provides more powerful ways of knowing what is going on at a "class" level as opposed to what transpires at an "object" level.

查看更多
劫难
7楼-- · 2020-06-30 05:55

Generally, yes. Static methods and properties use less memory. However, the difference is very small.

The more interesting thing is the performance difference between static and non-static methods.

查看更多
登录 后发表回答