是否有可能在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