從PHPmannual秒款,它說:PHP後期靜態綁定範圍混亂
static:: introduces its scope.
因此我嘗試了以下例子:
class Father {
public function test(){
echo static::$a;
}
}
class Son extends Father{
protected static $a='static forward scope';
public function test(){
parent::test();
}
}
$son = new Son();
$son->test(); // print "static forward scope"
它可以作爲描述。但是,下面的例子將引發一個致命的錯誤:
class Father {
public function test(){
echo static::$a;
}
}
class Son extends Father{
private static $a='static forward scope';
public function test(){
parent::test();
}
}
// print "Fatal erro: Cannot access private property Son::$a"
$son = new Son();
$son->test();
我的主要問題是如何解釋這裏所說的scope
?如果static
將Son
的範圍引入Father
,那麼爲什麼私有變量仍然不可見Father
?
是否有兩件事variable
範圍和visibility
範圍?如果這聽起來很有趣,我對PHP很抱歉。
你讀過這個https://stackoverflow.com/questions/1912902/what-exactly-are-late-static-bindings-in-php? –
儘管'引入範圍''私人'屬性是不可見的任何其他類。 –
@ ponury-kostek tks供參考。我讀過它。但我認爲我的問題與該主題不同。 – spacegoing