Keyword "use" in php

Asked

Viewed 599 times

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

Source: https://stackoverflow.com/questions/10965454/how-does-the-keyword-use-work-in-php-and-can-i-import-classes-with-it

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.

  • 2

    I think the answer to that question can clarify your doubt.

  • 2

    You can also help: https://answall.com/q/32467/101

  • 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.

  • So that’s it, 'use' in this case will make the variable accessible.

  • 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

1 answer

0


Use for anonymous function scope

We are using an anonymous function here

$push->on('incoming', function (\InstagramAPI\Push\Notification $push) use ($ig, $socket) {
    ...
});

When we use the words "use" in an anonymous function as in the example above, the variable passes to the scope of the anonymous function, but not as reference, if necessary pass as reference just do as the example below

$push->on('incoming', function (\InstagramAPI\Push\Notification &$push) use ($ig, $socket) {
    ...
});

So any change in the "$push" variable will be reflected externally.

Use in Namespace

use React\EventLoop\Timer\TimerInterface;

When we use "use" as namespace, we inform where it belongs, as in the example above, Timerinterface is inside Timer which in turn is inside Eventloop and finally inside React.

And for what this server? Imagine that there is another "Timerinterface", but it will belong to another place for example

use LugarMagico\TimerInterface;

But if we use both Timerinterface in the same place in this way

use React\EventLoop\Timer\TimerInterface;
use LugarMagico\TimerInterface;

$timer = new TimerInterface();

It will not be possible since as php will know which Timerinterface is what I’m calling, and to solve this is simple just do the following

use React\EventLoop\Timer\TimerInterface;
use LugarMagico\TimerInterface as TimerMagico;

$timer = new TimerInterface();
$timerMagico= new TimerMagico();

This way I assign a nickname, so php will know which one I’m calling

sources

I improved my answer thanks to Guilherme Nascimento, I answered according to my interpretation, but William replied masterfully in the following links below.

Namespaces and Use when to use and for what purpose?

What is the purpose of the "use" command and what is its relation to Anonimas functions?

  • Caro Wictor the use with namespaces has nothing to do with scope. use in classes is totally different from use in Closures ... The usein classes does not "anything", even with spl, in PHP it only serves to create nicknames, as I explained in https://answall.com/questions/151487/namespaces-e-use-quando-usar-e-para-que-servem/151492#151492 ... And about the use in closures, I explained here: https://answall.com/a/172134/3635. I hope it helps..

  • Thank you, @Guilhermenascimento, I edited the question :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.