【发布时间】:2012-03-15 19:41:32
【问题描述】:
是否可以在 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。
标签: php static callback anonymous-function self