4

I have been wondering for a while already how does static variables work regarding memory use and should that even be really considered?

I understand that static variables will only use up one area of memory, doesn't matter how many instances there are of the class itself. So in this sense, it should be wise to use static variables for wise memory consumption too, right? But I've never stumbled across anyone talking about the memory usage of static variables (only that you can share the data with different instances).

For example:

class Something () {
    static $DB = null;
    __construct ($DB) {
        $this->DB = $DB;
    }
}

If I would create 10 instances of this class, then it would generate less memory usage, than with non-static $DB-variable, right?

And if it is so, is the effect so small, it doesn't really matter?

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
Hachi
  • 536
  • 6
  • 17

4 Answers4

3

and should that even be really considered?

No you shouldn't worry about statics for that reason.

The reason you have to worry about the use of static is the fact that you cannot unit test your code anymore and you have tightly coupled classes and code to Something::DB (i.e. the Something class) and you are working with global state.

Also check out an previous answer by me about how to handle those "global" instances: Which is the best practice to access config inside a function?

Community
  • 1
  • 1
PeeHaa
  • 71,436
  • 58
  • 190
  • 262
  • Ok. So it's pretty close to what I expected. Since I'm writing the code alone and not unit testing it, it makes it difficult for me to see some bad coding habits. Thank you for the clarification, I get the idea now. – Hachi Jul 25 '12 at 09:47
  • 2
    @PeeHaa - You can do unit testing with stgatic variables. Just make them protected and then create a class that inherits them. In that class that you can test against you can add the functionality to reset those static variables. – Ed Heal Jul 25 '12 at 09:49
  • And how are you going to mock them when they are used in another class? Can you say dependency injection a.k.a the Hollywood principle? – PeeHaa Jul 25 '12 at 09:50
2

In your case, please, rethink your software design. In case of using static variables - you are trying (if its not, so why you need static?) to make something accessible from one place, without recreating it, like using Singleton pattern for making single instance of db object.

But if we are talking about memory usage, so yes, if you will create more objects, so you duplicating the variables - it will take more memory, but there are no real change in memory usage about its static or not.

Anton
  • 1,029
  • 7
  • 19
  • My problem is really that I have some global settings / variables (for example Database). This variable can be a default in the classes that use variables, but so far I've only come up with using the parameters to pass the DB-object, to all the different classes that use the DB-object and that seems like a waste. For example, if I have a class for authentication, internationalization and debugging and want all of them and the main index-page to use DB. What other possibilities do i have, than to pass the DB-variable to the constructor? – Hachi Jul 25 '12 at 10:01
  • You can use Singleton pattern, as i told. By realizing your database using with such design, you can use it anywhere by calling for example DB::query(..) or DB::getAll().. Please, read about Singleton pattern by using google. – Anton Jul 26 '12 at 07:27
1

Yes, a static attribute of a class would be stored in a single instance of memory.

But, that is not a concern in making a decision in having a variable as static. They are used for class level information such as to keep a count of the instances of a class.

Go through the following Stackoverflow post on when to use static variables:

Community
  • 1
  • 1
Ozair Kafray
  • 13,351
  • 8
  • 59
  • 84
  • Woah. That's a terrible answer you linked there. – PeeHaa Jul 25 '12 at 09:46
  • I haven't linked it to any answer. I have linked it to a question which has many answers. Sort them by votes, and they describe when to us static variables. I am sure there will be other SO threads on this topic too. – Ozair Kafray Jul 25 '12 at 10:15
  • 1
    Thank you for the link Ozair! I did search quite fairly, but never really found a good thread like that. – Hachi Jul 25 '12 at 10:23
0
  1. You should use

    self::$DB

to access static variables (as $this has no meaning in a class wide context)

  1. Should use static for something that all objects of that class share.

  2. You should not use parameters from the constructor to create static variables. Doing so the static variable gets overwritten when you create a new object of that type

Ed Heal
  • 59,252
  • 17
  • 87
  • 127