2

我有以下结构

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:: 和 get_class() 没有成功。

4

2 回答 2

3

变化self::$a_update()

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";
于 2012-07-18T16:35:19.190 回答
1

查看我的代码

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.
于 2013-07-09T12:16:03.557 回答