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";
}

Update関数が$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 に答える