2016-11-02 2 views
0

私は私がPHPへのアクセスの子供の保護値

class SuperClass{ 
    static protected $message = "This is the parent"; 

    public static function showMessage(){ 
     echo self::$message."<br/>"; 
    } 
} 

class SubClass1 extends SuperClass { 
    static protected $message = "This is the first child"; 

} 

class SubClass2 extends SuperClass { 
    static protected $message = "This is the second child"; 
} 

SuperClass::showMessage(); 
SubClass1::showMessage(); 
SubClass2::showMessage(); 

私が見ることを期待する

This is the parent 
This is the first child 
This is the second child 

しかし、プロパティに定義する親と子でメソッドの抽象化をしたい、このコードを持っているもの私は持っています

This is the parent 
This is the parent 
This is the parent 

答えて

1

これは非常に古典的な使用例ですlate static bindingです。 は、単にこれは、PHP 5.3+

+0

なるほどために働くだろう「静的」

class SuperClass{ static protected $message = "This is the parent"; public static function showMessage(){ echo static::$message."<br/>"; } } 

で親クラスのキーワード「自己」を交換してください!これだよ!どうも :) –

関連する問題