2

Suppose a class, foo, has one static variable, bar. Is it possible to write PHP code that will create a new static variable, bar2, for the foo class at runtime?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
johnjohn
  • 4,221
  • 7
  • 36
  • 46
  • Can you elaborate on why exactly you are wanting to do this? – TaylorOtwell Apr 07 '11 at 20:53
  • random guess: `static Foo::bar2 = 'baz'`? – Marc B Apr 07 '11 at 20:53
  • @TaylorOtwell: For convenience. If it was possible, I could create globally accessed objects dynamically. – johnjohn Apr 07 '11 at 20:57
  • 2
    @johnjohn: I think you really don't want to do that (using globals). It's pretty much an anti-pattern. It brings more harm than good. – netcoder Apr 07 '11 at 21:00
  • @johnjohn netcoder is right - you should never need to. Note, however, that you could have a static associative array and use that instead. – Hamish Apr 07 '11 at 21:03
  • @netcoder: Generally, I totally agree with you. But I wanted this for a homebrew framework, so that a user could modify the core class on runtime and access it from anywhere without `global $core;`. In its context, it would be great. – johnjohn Apr 07 '11 at 21:04

1 Answers1

7

No, it's not possible.

A static variable is, as its name says, allocated statically at compile time. It cannot be allocated during runtime, nor can it be deallocated (e.g.: unset) during runtime either.

Also, static variables are independent of the call stack.

You can read more on Wikipedia.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
netcoder
  • 66,435
  • 19
  • 125
  • 142
  • 1
    @nikic: No they don't. A static **local** variable is one thing. But a static variable is still a static variable. – netcoder Apr 07 '11 at 20:58
  • @netcoder: I still think that your statement isn't entirely correct. Static variables in PHP aren't really about memory being allocated at compile time (which sounds already pretty strange if a context of an interpreted, dynamically types language, where neither compilation happens, nor data structures can be really allocated in advance (maybe the `zval` may be preallocated...). But I revoked by downvoted as a +1 to pointing out that my code was incorrect. – NikiC Apr 07 '11 at 21:09
  • @nikic: Whether the language is interpreted (PHP, Java, Python) or not (C, Assembly), a compiler is still involved. Compilation actually means "to put together". – netcoder Apr 07 '11 at 21:48
  • @netcoder: It's a term one can argue about. But even if you assume that compilation is anything that happens before the program is executed, then still static variables have plainly nothing to do with what you linked on wikipedia. I actually checked the PHP source and basically the only difference between creating a static and a non-static variable, is that it is put into a different hash table. There is absolutely no difference in the way the values are initialized or allocated (in both cases only a zval is allocated). – NikiC Apr 08 '11 at 06:59
  • @nikic: No matter what the source code says, the definition remains the same: it can't be allocated or deallocated during runtime. Besides, [you use the term "compile time" yourself](http://stackoverflow.com/questions/2447791/define-vs-const/3193704#3193704). ;-) – netcoder Apr 08 '11 at 12:18
  • @netcoder: Point taken, I do use compile time myself ^^ Still, in my eyes in PHP a static variable is just a class variable and has nothing to do with the allocation behavior during compilation. But really, it isn't really important how the term "static" should be interpreted here, your statement that static variables can't be added or removed at runtime is correct and this is all that matters in the context of the question. You get a +1 for that ;) – NikiC Apr 08 '11 at 13:09