PHP闭包:绑定到静态类时,为什么要在匿名函数声明中使用静态? [英] php closures: why the 'static' in the anonymous function declaration when binding to static class?

查看:48
本文介绍了PHP闭包:绑定到静态类时,为什么要在匿名函数声明中使用静态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Closure::bind上的php文档中的示例在匿名函数声明中包含static。为什么?如果将其删除,我找不到区别。

class A {
    private static $sfoo = 1;
}
$cl1 = static function() { // notice the "static"
    return self::$sfoo;
};
$bcl1 = Closure::bind($cl1, null, 'A');
echo $bcl1(); // output: 1

不带

class A {
    private static $sfoo = 1;
}
$cl1 = function() {
    return self::$sfoo;
};
$bcl1 = Closure::bind($cl1, null, 'A');
echo $bcl1(); // output: 1

推荐答案

如您所见,这并不重要。

这类似于在类方法上使用static关键字。如果不在方法内引用$this不一定需要(尽管违反了严格的标准)。

我想,由于bind()的第二个参数nullClosure静态访问A的意思是Closure

这篇关于PHP闭包:绑定到静态类时,为什么要在匿名函数声明中使用静态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆