I know that in javascript
it is very useful to use self invoking functions
, especially when it comes to variable scope protection.
I was doing tests on PHP 7
and I see that we have the same possibility of using Self Invoking Function
- functions that can be called at the same time that they are declared.
Example:
$gen = (function() {
yield 1;
yield 2;
return 3;
})()
foreach ($gen as $number) {
echo $number;
}
The output is:
1
2
3
In this case using the Generator
, I see a lot of usefulness.
What are some other possible benefits of using Self Invoking Function
in PHP7
?