我有以下结构
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";
}
我希望Update函数也能返回$a,但我无法让它工作。
Bar::Load(); //returns child, Correct.
$bar = new Bar();
$bar->Update(); //returns parent, Wrong.
我尝试过self:、static:和get_class(),但都没有成功。
更改update()
中的self::$a
class Foo
{
protected static $a = "parent"; // Notice this is now "protected"
public function child()
{
return static::$a;
}
public function parent()
{
return self::$a;
}
}
class Bar extends Foo
{
protected static $a = "child"; // Notice this is now "protected"
}
$bar = new Bar();
print $bar->child() . "'n";
print $bar->parent() . "'n";
查看我的代码
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.