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.
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:
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.
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.
Checkout more details here: https://stackoverflow.com/a/3419411/260080
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.
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.
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.
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.