2
I was using an api when I came across the following code:
$push->on('incoming', function (\InstagramAPI\Push\Notification $push) use ($ig, $socket) {
...
});
I realized that the word "use" serves to use a certain variable within the scope of that internal function, but it was just a conclusion that I came to, what I wanted to know is, if it’s just that and final point or if there’s something else.
When I search for 'use' in php only appears on 'use' to import classes, for example:
use React\EventLoop\Timer\TimerInterface;
The closest I got to the answer was this:
use doesn’t include Anything. It just Imports the specified namespace (or class) to the Current Scope
But it only talks about classes and namespace nothing about variables, I wanted to know more about this word 'use', because I’m having some difficulties to use this api and it uses this resource a lot and I don’t want to do something wrong by not knowing how to use the resource.
I think the answer to that question can clarify your doubt.
– gato
You can also help: https://answall.com/q/32467/101
– Maniero
As you can see in the links that indicated above, there are two types of
use
in php. One is to import namespaces, and one is to capture variables from another scope (an implementation of closures). Your example is of this second type.– bfavaretto
So that’s it, 'use' in this case will make the variable accessible.
– Wictor Chaves
The message use doesn’t include Anything. It just Imports the specified namespace (or class) to the Current Scope I believe it was answered in https://answall.com/questions/151487/namespaces-e-use-quando-usar-e-para-que-servem/151492#151492
– Guilherme Nascimento