不要将$ this传递给匿名函数的范围(Do not pass $this into scope of an anonymous function)
有没有办法不将类传递给匿名函数?
class Foo{ public function bar(){ $data = [ 'calculation' => function(){ // I don't want $this to be passed into here } ]; } }我可以从匿名函数中排除$ this吗?
Is there a way to not pass the class into an anonymous function?
class Foo{ public function bar(){ $data = [ 'calculation' => function(){ // I don't want $this to be passed into here } ]; } }Can i exclude $this from that anonymous function?
最满意答案
如果你不想要$this的值,你需要一个静态匿名函数 。
class Foo{ public function bar(){ $data = [ 'calculation' => static function(){ // $this is not defined } ]; } }静态匿名函数没有自动绑定的$this值,并且以后不能将其他值绑定到它们。
If you don't want a value for $this, you want a static anonymous function.
class Foo{ public function bar(){ $data = [ 'calculation' => static function(){ // $this is not defined } ]; } }Static anonymous functions do not have a $this value automatically bound and they cannot have another value bound to them later on.
更多推荐
发布评论