Php 在非静态方法+继承中访问静态变量
我有以下结构Php 在非静态方法+继承中访问静态变量,php,oop,inheritance,static,non-static,Php,Oop,Inheritance,Static,Non Static,我有以下结构 class Foo { public static $a = "parent"; public static function Load() { return static::$a; } public function Update() { return self::$a; } } class Bar extends Foo { private static $a = "chi
class Foo
{
public static $a = "parent";
public static function Load()
{
return static::$a;
}
public function Update()
{
return self::$a;
}
}
class Bar extends Foo
{
private static $a = "child";
}
我希望更新函数也能返回$a,但我无法让它工作
Bar::Load(); //returns child, Correct.
$bar = new Bar();
$bar->Update(); //returns parent, Wrong.
我尝试了self:,static:,但没有成功。在更新中更改self:$a
更改自身::$a在更新中
看到我的代码了吗
class Foo
{
protected static $a = "parent";
public static function Load()
{
return static::$a;
}
public function Update()
{
return static::$a;
}
}
class Bar extends Foo
{
protected static $a = "child";
}
Bar::Load(); //returns child, Correct.
$bar = new Bar();
$bar->Update(); //returns child, Correct.
看到我的代码了吗
class Foo
{
protected static $a = "parent";
public static function Load()
{
return static::$a;
}
public function Update()
{
return static::$a;
}
}
class Bar extends Foo
{
protected static $a = "child";
}
Bar::Load(); //returns child, Correct.
$bar = new Bar();
$bar->Update(); //returns child, Correct.
当$a在父类中是公共的时,它被声明为私有的,这难道不是真正的错误吗?@tigrang,我也想知道这一点,但是如果他们没有得到任何错误,那么这只是一个建议/严格的更改,不应该影响实际的执行。我正在尝试使用php 5.4和5.3.10,因此得到一个致命的错误。当$a在父类中是公共的时,它被声明为私有的,这难道不是真正的错误吗?@tigrang,我也想知道这一点,但是如果他们没有得到任何错误,那么这只是一个建议/严格的更改,不应该影响实际的执行。我正在尝试使用php 5.4和5.3.10,因此得到一个致命的错误。