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.
Creating an instance everytime there are none equals recreating the entire object, static methods and variables generally consumes less memory depending on how they are used. Of course if you only need to create one instance throughout your entire program, there's no difference.
Besides static methods are difficult to test because of these tightly-coupled dependencies, which oftentimes lead to third-party infrastructure that the code depends upon - like a database, and it makes it very difficult to change the behavior without actually going in and changing the code.
It's generally a bad idea to depend on static methods because they make substitution (and therefore specialising or testing) difficult. It looks like what you have is 10 independent classes that all inherit from the same interface. But without explaining what those functions do, its impossible to say how to improve.
They are faster — Static methods are slightly faster than instance methods because in instance methods, you are also working with an implicit this parameter. Eliminating that parameter gives a slight performance boost in most programming languages.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With