PHP Double parenthesis in a variable declaration?

165
September 10, 2018, at 04:40 AM

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).

Answer 1

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+

READ ALSO
Select in mySqli not returning sort correctly and PHP not distinguishing differences

Select in mySqli not returning sort correctly and PHP not distinguishing differences

I have a table that contains over 16,000 records from a certain type of businessI am querying that table to get every city on file but I'm only displaying the city once with the number of instances in which the business exists in that city

160
Verify mobile number in phone? [on hold]

Verify mobile number in phone? [on hold]

How can I verify mobile numberin php?

86
Twig template how to use the {% flush %} block

Twig template how to use the {% flush %} block

I'm trying to figure out how to use the flush block on a twig template

94