开发者

do extended classes inherit static var values (PHP)?

开发者 https://www.devze.com 2023-02-12 00:38 出处:网络
If I have a base class that contains a static var, I then set 开发者_Python百科this static var, and then have a class that extends the base class, will the extended class retain the value of the stati

If I have a base class that contains a static var, I then set 开发者_Python百科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?


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号