我在PHP中有以下代碼 5.6.2。它有類Father
,Guy extends Father
和Child extends Guy
。所有這些類有一個靜態方法hi
其輸出類的名稱:PHP中的靜態匿名函數的意外範圍
class Father {
static function hi() {
echo "Father" . PHP_EOL;
}
}
class Guy extends Father {
static function hi() {
echo "Guy" . PHP_EOL;
}
static function test() {
self::hi();
static::hi();
parent::hi();
$anon = function() {
self::hi(); // shouldn't this call Guy::hi()?
static::hi();
parent::hi(); // shouldn't this call Father::hi()?
};
$anon();
}
}
class Child extends Guy {
static function hi() {
echo "Child" . PHP_EOL;
}
}
Child::test();
我期望的輸出是:被如預期
Guy
Child
Father
Guy
Child
Father
前三行。但最後三個令人驚訝的是:
Child //shouldn't this call Guy::hi()?
Child
Father //shouldn't this call Father::hi()?
如此看來,匿名函數$anon
有Child
範圍。但是它不應該和它被調用的方法有相同的範圍(即Guy
)?
編輯1:此外,不會 the specification需要這個工作,如我所料:
一個實例或靜態方法中定義的匿名函數有其範圍設置爲類是定義在其中。否則,匿名函數是不可見的。
編輯2:需要注意的是,當我從
Guy::test()
刪除
static
修改並調用它像
(new Child)->test();
輸出符合市場預期。
編輯3:期待一些更奇怪的行爲之後,我覺得這是在PHP確實是個bug -> according bug report
得到致命錯誤:當無級範圍上線
積極的,如果我執行你的代碼 – Freez 2015-02-07 18:44:36
@KarolyHorvath我不希望'不能訪問自己:: ::喜()'要打印但要在另一個類上調用(請參閱編輯) – MinecraftShamrock 2015-02-07 18:46:00
@Freez我使用http://sandbox.onlinephpfunctions.com/使用PHP 5.6.2來測試 – MinecraftShamrock 2015-02-07 18:46:26