是否有可能在PHP中使用匿名回調函數訪問self
,static
和$this
中的類/對象?就像這樣:
class Foo {
const BAZ = 5;
public static function bar() {
echo self::BAZ; // it works OK
array_filter(array(1,3,5), function($number) /* use(self) */ {
return $number !== self::BAZ; // I cannot access self from here
});
}
}
有沒有什麼辦法讓它表現爲與通常的變量,使用use(self)
條款?
從PHP 5.4開始,可以使用$ this。 – 2012-02-25 17:39:37
*(相關)* [PHP 5.4 - '封$這種支持'(http://stackoverflow.com/questions/5734011/php-5-4-closure-this-support/5734109#5734109) – Gordon 2012-02-25 17:52:11