goa*_*oat 25
所述use statement捕获变量在时间创建闭合功能.
常规函数参数在调用函数时捕获值.
请注意,我区分variable和value那里.
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
Run Code Online (Sandbox Code Playgroud)
如果有办法检查$add5to函数的"源代码" ,它看起来像这样:
function($rightNum) {
return 5 + $rightNum;
}
Run Code Online (Sandbox Code Playgroud)
我想你可以说$leftNum闭合函数记住的值.
我想进一步强调的是,use statement可以让你保持reference一个变量,而不仅仅是副本值的变量必须在某个时候.澄清我的想法:将变量视为一个小盒子,它可以在任何时刻包含单个值,并且可以更改该值.并且,您可以将另一个变量指向该框,以便您可以更新框中的值,或者读取其中的当前值.
通常,在函数返回后,函数内创建的局部变量不再存在.但是,闭包函数可以保持对该变量的引用,并且即使在函数返回后也会使该局部变量继续存在 - 这是闭包函数的真正功能.它允许您模拟一个类(实例变量)的某些行为,只需要一点点代码.
这是一个更高级的例子,可能需要深入思考才能理解行为的细节.
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.
Run Code Online (Sandbox Code Playgroud)
您可能已经注意到我在此示例中使用了引用运算符 &.如果您还不熟悉它们,请知道参考文献很难理解.虽然,我希望这篇文章本身最有意义.
sta*_*tan 22
闭包是一个在自己的环境中计算的函数,它有一个或多个绑定变量,可以在调用函数时访问它们.它们来自功能编程世界,其中有许多概念在起作用.闭包类似于lambda函数,但在某种意义上它们更聪明,它们能够与定义闭包的外部环境中的变量进行交互.
use()关键字让你从函数内部的函数环境中导入变量.要从外部环境导入的变量在闭包函数定义的use子句中指定.默认情况下,它们按值传递.因此,假设该函数没有参数,但您不想使用已有的变量.
$string = "Hello World!";
$closure = function() use ($string) { echo $string; };
Run Code Online (Sandbox Code Playgroud)
当您需要创建一个必须在其他地方用作回调的函数时,这非常有用,并且只能定义参数.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);
}
Run Code Online (Sandbox Code Playgroud)
$ callback必须只有两个参数,因为array_walk只允许那么多:
通常,funcname采用两个参数.数组参数的值是第一个,键/索引的第二个.
所以,我们能做些什么?我们调用use()添加其他不是$ callback范围的变量,但是在调用它的环境范围内.