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

Asked

Viewed 1,523 times

11

I am doing some tests with a Restful API that I am creating using the Slim micro-framework, and in one of these routines that is responsible for executing a certain action caught my attention a command that I did not know, which is the command use.

See below for an example of the command use in a method that returns a JSON with the data obtained from the database:

//Listando todas pessoas
$app->get('/pessoas/', 
        function() use ($app) 
        {
            (new \controllers\Pessoa($app))->listar();            
        });

This command seems to be part of the anonymity function function() who is responsible for the route:

http://localhost/RestApiEx/pessoas/

And the use of it left me with the following doubts.

Doubts

I’d like to know what the purpose of command is use and what is the relationship that it has with Anonimas functions?

1 answer

9


The use passes the value of one or more variables to the scope of the anonymity function making this value accessible (inheriting the variable), for example if you do this:

<?php

$a = 'Olá, mundo!';

$foo = function() {
   echo $a;
};

$foo();

You’ll find that the variable was not defined, something like:

PHP Notice: Undefined variable: a in example.php on line 6

But if I do this:

<?php

$a = 'Olá, mundo!';

$foo = function() use ($a) {
   echo $a;
};

$foo();

The value will be printed 'Olá, mundo!'

In the framework Slim the main variable of the application is the $app, but to make it accessible to the scope of the anonymity function use, if I did this:

$app->get('/pessoas/', 
        function() 
        {
            (new \controllers\Pessoa($app))->listar();            
        });

Would have a similar mistake:

PHP Notice: Undefined variable: app in exemplo.php on line 9

And your controller would get a value null instead of the object new \Slim\App;

Doc: http://php.net/manual/en/functions.anonymous.php

Extras

  1. A detail when using use the variable $a example is not a reference, if you do something like:

    <?php
    
    $a = 1;
    
    $foo = function() use ($a) {
       $a += 10;
    };
    
    $foo();
    
    echo $a;
    

    Will be shown:

    1

    But if you make the reference:

    <?php
    
    $a = 1;
    
    $foo = function() use (&$a) {
       $a += 10;
    };
    
    $foo();
    
    echo $a;
    

    Will be shown 11:

    11

  2. Something important to note is that in PHP there is another use, which is used to create nicknames for classes, thus:

    <?php
    
    use Foo\Bar\Baz; //é possivel chamar new Baz, sem o namespace completo
    use Foo\Bar\Baz as Test; //é possivel chamar new Test para se referir ao Foo\Bar\Baz
    

    Read more on /a/151492/3635

  • 1

    I think in the case of the example of Slim, there could also be an example without the error with the use of the ùse`, for better understanding.

  • @Magichat as well?

  • You gave an example using the framework as reference Slim, but in your example there is no use of the operator use, which generates an error, as would be the example with the use of use...

  • @Magichat is because you already have the example in the question (http://answall.com/q/172133/3635), the context was to explain what would happen if I removed ;)

  • It may be hard to believe, but before I put the comment I saw and soon came to mind : "English Putz and I still have to read everything until I find the example".... hehe Tavez if the link led straight to #id of the example..

  • @Magichat I put the doc link in English, because the php doc is community, so the English version is better maintained, already the Portuguese version is full of gaffes, I prefer the link in English that teaches correct to in Portuguese that teaches wrong ;D

  • I can use the use to pass the value of the variable to function, but it will not change the value of the variable, ok. But if I reference &, then I change its value. What’s the difference in using the use with reference to change the value of the "external variable", to pass a property to function, to give the return setting itself? It would have advantages in organization or execution, or we have cases that only using the use and reference would work?

  • @Rbz o use has nothing to do with the reference, use only "copy" the values into the scope of an anonymized/lambda function, see example: https://ideone.com/kpcLO5

  • Yes, so far ok, which is this far: "I can use to pass the value of the variable to function, but it will not change the value of the variable, ok. But if I reference &, then I change its value.". Doubt is more than "why/when" to use use with or without reference, comparing if I can pass as function property. If there are cases that would only work with the use with or without reference, or reference only, or if it is just another way of doing...

Show 4 more comments

Browser other questions tagged

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