do extended classes inherit static var values (PHP)?

坚强是说给别人听的谎言 提交于 2020-01-15 11:19:06

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/5059525/do-extended-classes-inherit-static-var-values-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!