本文介绍了扩展类是否继承静态var值(PHP)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有一个包含静态var的基类,则设置此静态var,然后再扩展该基类的类,扩展后的类将保留我已经在其中设置的静态var的值.基类?
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?
推荐答案
是的,尽管它们是不同的变量,但两个类中的静态变量都在同一引用集中.
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
这篇关于扩展类是否继承静态var值(PHP)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!