PHP变量声明中的双括号?

In a exercise of my teacher he wrote the next declaration:

$gen = (function(){
        yield 1;
        yield 2;
        return 3;
})();

He was talking about "yield", but I really don't understand the function of the second parenthesis (before the last semicolon).

Like JavaScript it allows you to make use of arguments for example

$gen = (function($a){
   echo "this is $a";
})('a');

Output

 this is a

Test it yourself Sandbox

In Javascript I use something like this

 (function($){

 })(jQuery);

When jQuery is in compatibility mode. It does the same thing, it puts jQuery into the $. Same deal.

UPDATE

I can explain these another way, you could do this

$a = new Object($b);
$b->doSomething();

Right that's perfectly understandable, what about this one:

    //call a method of an object without setting a local variable
   (new Object($b))->doSomething();

Well with a closure it calls __invoke as the method (basically), Or it knows to call it if you try to execute an object as a method/function.

    (function($a){echo "this is $a";})('a');

This can be show easily too.

class foo{ 
    public function __invoke($e){
        echo $e;
    } 
}

(new foo)("hello");
//these are all equivalent to the above, and work.

$foo = new foo;
//call invoke directly   
$foo->__invoke("hello");
//remember Foo is an instance of Foo. so this calls __invoke magic method
//call invoke indirectly
$foo("hello");

//then in the same way this (new object) works and the above works then
(new foo)->__invoke("hello");
//this also works
(new foo)("hello");

Output

hello

Sandbox

Basically, PHP knows that a anonymous function call should create a closure object, and then when you call it with the (function ... ) brackets it calls the __invoke magic method that is the function of the closure. And then in the same way doing $f('hello') the (function ...)('hello') does pretty much the same thing.

Now I don't know if that is exactly what happens under the hood, so to speak, but functionally that's the way it works.

Hope that makes sense.

One last thing is some of these only work in the newer versions of PHP, I would have to look up each permutation to find out exactly when PHP implement them. But they all work in PHP7+, I think closures were added in PHP5.3 but the scope was wrong on them then, until PHP5.4 (couldn't use $this inside them). I think 5.6 added the (new Object) syntax but I could be wrong on that. And I am pretty sure the implicit invoke (Obj)() was added in PHP7.

The major versions I have personally done a lot of work with are 4.4, 5.0 5.3,5.6 and 7+