2

I've seen the function () use ($app) { syntax done over and over again in the Lumen docs here.

The full syntax looks like that:

$app->group(['middleware' => 'auth'], function () use ($app) {
    $app->get('/', function ()    {
        // Uses Auth Middleware
    });
});

Is this thing somehow related to the PHP? Lumen? Is it also available in the Laravel?

It looks like the anonymous function in PHP without the curly brackets, however, the use keyword doesn't make sense in the context of this particular code example. As far as I know, using use could be like an alias or the trait in the context of OOP.

Tried changing it a little bit, because I'm not a huge fan of the function () :D My attempt using function () { use ($app) { results in the syntax error.

I haven't seen anything like that in the PHP before, could you give me some details about it?

mega6382
  • 9,211
  • 17
  • 48
  • 69
  • The `use` keyword has a different meaning in this context. You are right it's also used with namespaces. I'll leave you with the [manual](http://php.net/manual/en/functions.anonymous.php); scroll down to example 3. I think it explains it well. – jh1711 Aug 07 '18 at 22:12
  • Thank you :) It's a late night right now, so gonna do it in the morning :) I wish I could upvote your comment. – Puka Unknown Aug 07 '18 at 22:18

1 Answers1

5

When in a closure function (any function which closes over the environment in which it was defined) you need to make use of an external variable you use the use ($foo, $var, ...) to make them available inside the function.

For example, the next closure function would throw an exception:

    $name = 'Mark';

    \DB::table('users')
        ->where(function ($query) {
            $query->where('email', 'some_email')
                ->orWhere('name', $name) // <- $username doesn't exist here
        });

because $name is not defined inside the closure function.

That's when use comes in handy:

    $name = 'Mark';

    \DB::table('users')
        ->where(function ($query) use ($name){
            $query->where('email', 'some_email')
                ->orWhere('name', $name) // <- now it's available
        });

Returning to your original question, that isn't exclusive to Lumen, I've just checked and it is also present in Laravel apps (same family, so not a surprise). I think that the $app represents the Lumen/Laravel's Service Container (read this for more info: Understanding the Laravel Service Container) and that is needed in this case to register routes and make them available everywhere (inside the app).

Kenny Horna
  • 13,485
  • 4
  • 44
  • 71
  • Fun fact: Despite the fact that PHP named the class "Closure" they're not actually Closures, as defined by any other language with them, since they don't enclose the surrounding scope. PHP has Anonymous Functions, otherwise known as Lambdas. – Sammitch Aug 08 '18 at 01:19
  • @Sammitch I didn't know that. Actually, I've read in several places that they are indeed closures -like [here](https://www.culttt.com/2013/03/25/what-are-php-lambdas-and-closures/) or [here](https://stackoverflow.com/questions/4912116/closure-vs-anonymous-function-difference) for example- but we can learn something new everyday :+1 . By the way, do you have an article that support what you say? It is really interesting your observation, I'd like to digg a little bit deeper. – Kenny Horna Aug 08 '18 at 01:29
  • That's a pretty nice solution! :) I've been dealing with this problems when I can't access the outer scope variables and been using a little different approach: `$whyUndefined = '..s/../wp-load.php'; function b($whyUndefined) { return $whyUndefined; } echo b($whyUndefined); // ..s/../wp-load.php`. Is this the same? – Puka Unknown Aug 08 '18 at 08:39
  • @HCK no article per se, but lots of experience answering questions like "why can't I access this variable in my PHP closure?" and "why does PHP call the class 'Closure' when it's not one?", and then people with Java/Python/Ruby/etc backgrounds ranting on IRC :P – Sammitch Aug 08 '18 at 17:58