0

I'm trying to figure out what static vars are.

They can be access without instantiating the class but what other benefits do they have and when should they be used?

For example, my class has a private var which holds the name of the twitter feed i'm trying to get.

Should this be static? It never needs to change.

halfer
  • 19,824
  • 17
  • 99
  • 186
panthro
  • 22,779
  • 66
  • 183
  • 324
  • Not a duplicate. Im asking about vars, not methods. – panthro Nov 11 '13 at 14:27
  • If it never needs to change, why not use a `constant`? – h2ooooooo Nov 11 '13 at 14:28
  • If it never needs to change, make it a constant. A twitter feed class, on the other hand, can have multiple instances which all refer to a different feed, so that would be a normal instance variable (called 'property' in PHP). – GolezTrol Nov 11 '13 at 14:28
  • @j08691 He's talking about static variables, not static methods. Static variables are for when you want a variable inside a function to keep it's value if the function is called again. – Ben Fortune Nov 11 '13 at 14:28
  • You might also benefit from reading here: [http://stackoverflow.com/questions/10795502/what-is-the-use-of-static-variable-in-cwhen-to-use-itwhy-cant-i-declare-the-s][1] [1]: http://stackoverflow.com/questions/10795502/what-is-the-use-of-static-variable-in-cwhen-to-use-itwhy-cant-i-declare-the-s – mcwyrm Nov 11 '13 at 14:30

4 Answers4

1

Generally things which aren't instance specific but needs to be stored in a variable should be static variables. Otherwise this manual tells the details: http://php.net/manual/en/language.variables.scope.php

Otherwise you can consider using constants also. For the example you mentioned (as others wrote) using constants seems to be the most sensible. (Either a class constant, or simple one.)

Lajos Veres
  • 13,595
  • 7
  • 43
  • 56
0

Static variables are for when you want a variable inside a function to keep it's value if the function is called again.

An example of a static variable could be the following.

function addOne(){
    static $i = 0;
    $i++;
    return $i;
}

echo addOne();
echo addOne();
echo addOne();

Which would return

123

Without the static keyword, this would simply return

111

In your question, you mention you have data that won't need to be changed. As the comments in the question state, you should make this a Constant.

Ben Fortune
  • 31,623
  • 10
  • 79
  • 80
  • Thanks for the explanation. Very good. As for constants, why would I use one over a private var? I don't need access to the var outside of the class? Which would be the best to use? – panthro Nov 11 '13 at 14:41
  • If you're certain it won't need to be changed, then a constant will do just fine. Private variables are mainly used to transport data around the class, passing from one function to another with the ability for the variable to change. – Ben Fortune Nov 11 '13 at 14:45
  • Also useful if you're extending a parent class... for instance I have a set of small dictionaries for handling multilingual exceptions. I set the static `$sLangCode` variable once in the parent (based on the current locale the application is running in) in an `app.ini.php` type file, then whenever the system throws an exception, no matter the TYPE of exception, because they all extend that base class (which itself extends `Exception`) all error messages are properly localised... if you see what I mean. – CD001 Nov 11 '13 at 14:58
0

In short, static variables can be used for constants.

For example, a Math class can have static variables; PI etc.

0

Let's say you have something in a class that you need later.

Now, you need that thing but you don't actually need|want|should create a new instance of that class.

That's why you use a static method/property

Guillermo Mansilla
  • 3,779
  • 2
  • 29
  • 34