どもです。
static変数、便利ですね。僕も大好きです。
class Paren{
static $a = '親のstatic変数';
static function test2(){
var_dump(self::$a);
}
}
class Child extends Paren{
static $a = '子のstatic変数';
static function test1(){
var_dump(self::$a);
}
}
Child::test1();
Child::test2();
実行結果
string(18) "子のstatic変数"
string(18) "親のstatic変数"
string(18) "子のstatic変数"
string(18) "親のstatic変数"
_ , ,_ パァン
( ‘д‘)
⊂彡☆))Д´)
継承先を指定して実行した関数で、敢えて親のstatic変数が欲しいことなんて無いだろうが!!
class Paren{
static $a = '親のstatic変数';
static function test2(){
var_dump(static::$a);
}
}
class Child extends Paren{
static $a = '子のstatic変数';
static function test1(){
var_dump(static::$a);
}
}
Child::test1();
Child::test2();
実行結果
string(18) "子のstatic変数"
string(18) "子のstatic変数"
string(18) "子のstatic変数"
string(18) "子のstatic変数"
はい。
static変数を自身のクラス内から呼び出す場合、self::はダメです。static::を使おう。
【難しい話はこちら】PHPマニュアル 遅延静的束縛 (Late Static Bindings)