这让我非常困惑,我似乎找不到这个问题的答案。希望能提供一个清晰简单的解释。
这让我非常困惑,我似乎找不到这个问题的答案。希望能提供一个清晰简单的解释。
use语句
在闭包函数创建时捕获变量。变量
和值
。function makeAnAdder($leftNum) {
// Notice that *each time* this makeAnAdder function gets called, we
// create and then return a brand new closure function.
$closureFunc = function($rightNum) use ($leftNum) {
return $leftNum + $rightNum;
};
return $closureFunc;
}
$add5to = makeAnAdder(5);
$add7to = makeAnAdder(7);
echo $add5to(10); // 15
echo $add7to(1); // 8
$add5to
函数的 "源代码",它将会像这样:
function($rightNum) {
return 5 + $rightNum;
}
我想说的是,use语句
允许您维护对变量的引用,而不仅仅是该变量在某个时间点上所具有的值的副本。为了澄清我的想法:将变量视为一个小盒子,它可以在任何时刻包含单个值,并且该值可以更改。您可以使另一个变量指向该盒子,以便您可以更新盒子中的值或读取其中的当前值。
通常,在函数内创建的局部变量在函数返回后会停止存在。但是,闭包函数可以维护对该变量的引用,并使该局部变量即使在函数返回后也能继续存在 - 这就是闭包函数的真正威力。它让您只需写入少量代码即可模拟类(实例变量)的某些行为。
这里有一个更高级的示例,可能需要深入思考才能理解其行为的细节。
我猜你可以说闭包函数记住了$leftNum
的值。
function makeBankAccount() {
// Each time this makeBankAccount func is called, a new, totally
// independent local variable named $balance is created.
$balance = 0;
// Also, on each call we create 2 new closure functions, $modifyBalance, and $getBalance
// which will hold a reference to the $balance variable even after makeBankAccount returns.
$modifyBalance = function($amount) use (&$balance) {
$balance += $amount;
};
$getBalance = function() use (&$balance) {
return $balance;
};
// return both closure functions.
return ['modifyBalance' => $modifyBalance, 'getBalance' => $getBalance];
}
// Let's prove that bank1 works by adding 5 to the balance by using the first
// function, then using the other function to get the balance
// from the same internal variable.
$bank1 = makeBankAccount();
$bank1['modifyBalance'](5);
echo $bank1['getBalance'](); // 5 - it works.
// Now let's make another bank to prove that it has it's own independent internal $balance variable.
$bank2 = makeBankAccount();
$bank2['modifyBalance'](10);
echo $bank2['getBalance'](); // 10 - as expected. It would have printed 15 if bank2 shared a variable with bank1.
// Let's test bank1 one more time to be sure that bank2 didn't mess with it.
echo $bank1['getBalance'](); // 5 - still 5, as expected.
你可能已经注意到我在这个例子中使用了引用运算符&
。如果你还不熟悉它们,只需要知道引用被认为很难理解。尽管如此,我希望这篇文章本身大部分都是有意义的。
$rightnum
参数是如何被调用的,但现在我理解了。 - Seralize$string = "Hello World!";
$closure = function() use ($string) { echo $string; };
当您需要创建一个必须用作回调的函数,并且只能定义参数时,这将非常有用。使用use()关键字可以让您除了传递给函数的参数外,还可以使用其他变量。例如在php.net的示例中:http://php.net/manual/en/functions.anonymous.php
public function getTotal($tax)
{
$total = 0.00;
$callback =
function ($quantity, $product) use ($tax, &$total)
{
$pricePerItem = constant(__CLASS__ . "::PRICE_" .
strtoupper($product));
$total += ($pricePerItem * $quantity) * ($tax + 1.0);
};
array_walk($this->products, $callback);
return round($total, 2);
}
$callback必须只有两个参数,因为array_walk只允许这么多:
通常,funcname采用两个参数。数组参数的值是第一个参数,键/索引是第二个参数。
那我们该怎么办?我们调用use()
添加其他不在$callback作用域内但在它被调用的环境作用域内的变量。
$closure = function($string) { echo $string; };
有什么不同吗? - Seralizeuse()
关键字基本上只是一种将变量导入闭包的方法,而不会破坏“参数流”,因为未设置的参数会导致错误? - Seralize