Do extended classes inherit static var values ​​(PHP)?

If I have a base class containing a static var, then I install this static var and then have to have a class that extends the base class, will the extended class retain the value of the static var that I already set in the base class?

+1
source share
1 answer

Yes, although they are different variables, the static variables in both classes are in the same set of references.

You can break up this set of links using a reference assignment ( =&) or by updating it in an 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
+2
source

All Articles