PHP use() 函数的作用域?
我见过这样的代码:
function($cfg) use ($connections) {}
但 php.net 似乎没有提到该功能。我猜这与范围有关,但是如何呢?
I have seen code like this:
function($cfg) use ($connections) {}
but php.net doesn't seem to mention that function. I'm guessing it's related to scope, but how?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
use
不是一个函数,它是 闭包语法的一部分。它只是使外部作用域的指定变量在闭包内可用。例如:
use
is not a function, it's part of the Closure syntax. It simply makes the specified variables of the outer scope available inside the closure.For example:
它告诉匿名函数使
$connections
(一个父变量)在其范围内可用。如果没有它,
$connections
将不会在函数内部定义。文档。
It is telling the anonymous function to make
$connections
(a parent variable) available in its scope.Without it,
$connections
wouldn't be defined inside the function.Documentation.
只是对答案的补充
Just an addition to the answer