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?
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
@Magichat as well?
– Guilherme Nascimento
You gave an example using the framework as reference
Slim
, but in your example there is no use of the operatoruse
, which generates an error, as would be the example with the use ofuse
...– MagicHat
@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 ;)
– Guilherme Nascimento
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
@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
– Guilherme Nascimento
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 theuse
with reference to change the value of the "external variable", to pass a property to function, to give thereturn
setting itself? It would have advantages in organization or execution, or we have cases that only using theuse
and reference would work?– rbz
@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
– Guilherme Nascimento
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 theuse
with or without reference, or reference only, or if it is just another way of doing...– rbz