I was trying to borrow some programing paradigms from JS to PHP (just for fun). Is there a way of doing:
$a = (function(){
return 'a';
})();
I was thinking that with the combination of use
this can be a nice way to hide variables JS style
$a = (function(){
$hidden = 'a';
return function($new) use (&$hidden){
$hidden = $new;
return $hidden;
};
})();
right now I need to do:
$temp = function(){....};
$a = $temp();
It seems pointless...
Function Call Chaining, e.g.
foo()()
is in discussion for PHP5.4. Until then, usecall_user_func
:gives:
As of PHP7, you can immediately execute anonymous functions like this: