Home > Enterprise >  functions arguments vs use keyword arguement in anonymous function?
functions arguments vs use keyword arguement in anonymous function?

Time:05-02

so often we come across one point where we are using some closures in php and as I use laravel for my projects closures are used so much in frameworks like in eloquent queries

so here is a scenario

$name = "john doe";

$greet = function() use ($name) {
    echo "hi " . $name;
    echo "\n";
};

$greetAgain = function($name) {
    echo "hi " . $name;
};

$greet();


$greetAgain($name);

now the output i get is

hi john doe
hi john doe

see there is no difference at all but according to closures there are two approaches i've used one is

passed name in function's arguments

passed name in use keyword

but as there is no difference i just wanted to know what is the main reason behind these two different approaches for same result

yes I also know that use keyword is used because anonymous function can not have outer scope

also i just wanted to know when to use which one method

this question was also asked by me and it arise this question use identifier in php and how does it affect the logic

so any discussion any hints or any helps would be very appreciated

thank you .. hope question is appropriate.....

CodePudding user response:

The reason the two appear to do the same thing is that you have over-simplified your example, and used the function as soon as you create it. In real code, the point of creating an anonymous function would be to declare it in one place, and use it in another - for instance, to pass it as the callback to usort or array_map; or to re-use it multiple times.

It follows naturally that there are two different times you might have data that the function needs, and that's what the two parts of the declaration are for:

  • When you define the function, you can "capture" data using the use clause
  • When you execute the function, you can pass in data using the parameters

A better example that demonstrates the difference might be:

// We know the surname we want to use every time the function runs
// Maybe it actually comes from user input
// But we don't yet know what first name to use
$surname = "Smith";
$greet = function($firstname) use ($surname) {
    echo "hi " . $firstname . " " . $name;
    echo "\n";
};

// This might happen in a completely different part of the code
// We might no longer know the surname, but the closure remembers it for us
foreach ( ['John', 'Jane', 'Jimmy'] as $firstname ) {
    $greet($firstname);
}

CodePudding user response:

When you work with laravel eloquent for example they used closures a lot as you mention for example like this

User::with(['products' => function($q){
   $q->select('name');
}])->get();

in this example, I get all users with their products name only using closure let's say that I want to get the products of the user depending on request for example I want to get user products depending on their price

$price = request()->get('price');

User::with(['products' => function($q) use($price) {
   $q->select('name')->where('price', '>', $price);
}])->get();

now as you see I pass the $price by using use() I can't pass the $price variable as an argument here to the closure because the eloquent itself would invoke it not you.

I mean by that you couldn't say

$price = request()->get('price');

User::with(['products' => function($q, $price){ // the $price value will be empty
   $q->select('name')->where('price', '>', $price);
}])->get();
  • Related