Update: Starting with PHP7, it is now possible to use anonymous function dereferencing using the syntax:
$array[] = [
'new' => (function()
{
...
return mt_rand();
})(),
'or' => getClosure()()
]
Original post: I've recently experimenting with some things, and wondered if there was any way to use the return value of an anonymous function
Lets say I had a for-loop that made an array that each value of the array had to have a database call, something I would like to do is:
for($i = 0; $i != 10; $i++)
{
$array[] = [
'new' => function(){
// some proccesing here maybe
// lets use mt_rand for this example.
return mt_rand();
},
'old' => function(){
return mt_rand();
}
];
}
or maybe
echo function(){
// again, we'll just use mt_rand
return mt_rand();
};
These both return a closure class. Is there anyway to actually pass the return value of them back to the array or echo, for the examples above?
Update: I've established this isn't possible so, feature request can be found here: http://bugs.php.net/bug.php?id=64608