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
An anonymous method can return a value using a func delegate.
No, you cannot return a value from an asynchronous callback. Event callback will be called on click, but you can't assign its result.
An anonymous method is a method which doesn't contain any name which is introduced in C# 2.0. It is useful when the user wants to create an inline method and also wants to pass parameter in the anonymous method like other methods.
In Python, an anonymous function is a function that is defined without a name. While normal functions are defined using the def keyword in Python, anonymous functions are defined using the lambda keyword. Hence, anonymous functions are also called lambda functions.
Try assigning the anonymous function
to a variable.
$myFunc = function() {
return 'Test';
}
echo $myFunc(); // Outputs Test
The value of the function itself is not the return value. The return value is the value returned by the function when the function is called
.
Edit:
As suggested by deceze, you can use call_user_func()
. Another way to achieve what you want is to make use of php's eval()
, which is by no means a good coding practice.
$array[] = array(
'new' => call_user_func(function() {
// some proccesing here maybe
// lets use mt_rand for this example.
return mt_rand();
}),
'old' => call_user_func(function() {
return mt_rand();
}),
);
eval()
echo eval('$x = function() {
// some proccesing here maybe
// lets use mt_rand for this example.
return mt_rand();
}; return $x();');
Simplest workaround to date:
echo call_user_func(function () { return 'foo'; });
The closure appears to have to be assigned before it can be de-referenced - Try this below code:
for($i = 0; $i != 10; $i++)
{
$array[] = [
'new' => call_user_func(function(){
// some proccesing here maybe
// lets use mt_rand for this example.
return mt_rand();
}),
'old' => call_user_func(function(){
return mt_rand();
})
];
}
[edit] - Modified to use call_user_func() instead of custom function - doh!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With