This has got me very confused and I can't seem to find an answer to this question. A clear and simple clarification would be nice.
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
A closure is a function that is evaluated in its own environment, which has one or more bound variables that can be accessed when the function is called. They come from the functional programming world, where there are a number of concepts in play. Closures are like lambda functions, but smarter in the sense that they have the ability to interact with variables from the outside environment of where the closure is defined.
The use() keyword let's you import variables from outside the function environment, inside the function. Variables to be imported from the outside environment are specified in the use clause of the closure function definition. By default, they are passed by value. So let's say the function has no parameters, but you wan't it to use a variable you already have.
This is useful when you need to create a function what must be used as callback somewhere else, and can only have defined parameters. The use() keyword let's you use other variables in addition to the ones you pass as function arguements. For example on the php.net example: http://php.net/manual/en/functions.anonymous.php
$callback must only have two parameters, because array_walk will only allow that much:
So what can we do? We call
use()
to add other variables that are not the $callback's scope, but in scope of the environment it is being called in.The
use statement
captures the variable at the time the closure function is created.Regular function arguments capture the value when the function is called.
Note that I differentiated between
variable
andvalue
there.If there were a way to inspect the, um, "source code" of the
$add5to
function, it would look like this:I guess you could kinda say the value of
$leftNum
got remembered by the closure function.I want to further emphasize that the
use statement
allows you to maintain areference
to a variable, and not just a copy of the value that the variable had at some point. To clarify my idea: think of a variable as being a little box, which can contain a single value at any instant in time, and that value can be changed. And, you can make another variable point to that box, so that you can update the value in the box, or read the current value in it.Normally, a local variable that is created within a function ceases to exist after the function returns. But, a closure function can maintain a reference to that variable, and cause that local variable to live on even after the function returns - and this is the true power of closure functions. It lets you mimic certain behaviors of a class (instance variables), with just a tiny bit of code.
Here's a more advanced example, that might take some deep thinking to understand the fine details of the behavior.
You may have noticed I used the reference operator
&
in this example. If you're not yet familiar with them yet, just know that References are known to be hard to understand. Although, I hope this post mostly makes sense by itself.