If I have a base class that contains a static var, I then set this static var, and then have a class that extends the base class, will the extended class retain the value of the static var that I have already set in the base class?
Asked
Active
Viewed 1,145 times
1 Answers
2
Yes, although they're different variables, the static variables in both classes are in the same reference set.
You can break this reference set though, by using reference assignment (=&
) or by redeclaring it in the extended class:
class base {
public static $var;
}
class extended extends base {}
extended::$var = 8; // base::$var == 8
$t = 6;
extended::$var =& $t; // base::$var == 8; extended::$var == 6
class base {
public static $var;
}
class extended extends base {
public static $var;
}
extended::$var = 8; // base::$var == null; extended::$var == 8

Artefacto
- 96,375
- 17
- 202
- 225
-
-1 The declared `$var` properties above are not static, but you reference them as statics. Inherited static properties are shared with their subclasses. – David Harkness Feb 20 '11 at 20:09
-
@David Thanks, fixed. But that was obviously a typo since the question and the answer are both about static variables. – Artefacto Feb 20 '11 at 22:30
-
My point was rather that "they're different variables" is incorrect. AFAIT, subclasses inherit access to the public/protected static variables of their parent(s) without creating space for a new variable. However, your sample code works as advertised. Until you assign a reference to the subclass's `$var`, the variables are linked. Does every subclass internally copy the static variable declarations of its parent and set each to be a reference to the matching parent variable? – David Harkness Feb 20 '11 at 22:57
-
BTW, I tried to remove the -1, but SO says I cannot undo it since I voted over two hours ago *unless you edit the answer* which you did 30 minutes ago. :/ – David Harkness Feb 20 '11 at 23:00
-
@David "Does every subclass internally copy the static variable declarations of its parent and set each to be a reference to the matching parent variable?" Yes. It isn't true that, as you initially say, "subclasses inherit access [...] without creating space for a new variable". There is indeed a new variable, or at least a new *symbol*. But if you say there is no new variable, you are also saying that in `$a = 1; $b =& $a;` there is only one variable. – Artefacto Feb 20 '11 at 23:03
-
1@Artefacto - In the case of an inherited static variable, using `ReflectionProperty` shows the parent as the class, so it looks to me like a single symbol. However, that doesn't mean that's what's happening under the hood as your example demonstrates. ;) The case of `$a` and `$b` is more obvious; there are clearly two symbols involved. – David Harkness Feb 20 '11 at 23:22
-
1In fact, even after assigning the `$t` reference to `$var`, reflection shows `base` as the class for `$var` within `extended`. – David Harkness Feb 20 '11 at 23:24
-
@David `ReflectionProperty` only shows the class where the property was declared. It doesn't mean the property belongs to that class. – Artefacto Feb 21 '11 at 00:52
-
@Artefacto - Sure, but I was hoping that once you broke the reference to the parent's property, it would let you see that. All the reflection items are based solely on the class declarations so this makes sense. – David Harkness Feb 21 '11 at 06:20