不要将$ this传递给匿名函数的范围(Do not pass $this into scope of an anonymous function)

系统教程 行业动态 更新时间:2024-06-14 17:02:18
不要将$ 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.

更多推荐

本文发布于:2023-04-21 18:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/67c117b488d7e16cc8eeafba4fe49b00.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:要将   函数   pass   function   anonymous

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!