PHP - self,static或$ this in callback function

Pav*_* S. 15 php static callback self anonymous-function

是否可以访问被引用的类/对象self,static以及$thisPHP中的匿名回调?像这样:

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
         });
    }
}
Run Code Online (Sandbox Code Playgroud)

有没有办法让它像通常的变量一样使用use(self)子句?

Kin*_*nch 16

使用PHP5.4就可以了.现在,这是不可能的.但是,如果您只需要访问公共属性,方法

$that = $this;
function () use ($that) { echo $that->doSomething(); }
Run Code Online (Sandbox Code Playgroud)

对于常量,没有理由使用限定名称

function () { echo Classname::FOO; }
Run Code Online (Sandbox Code Playgroud)