Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anonymous Function return value

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

like image 283
Jordan Doyle Avatar asked Apr 07 '13 22:04

Jordan Doyle


People also ask

Can an anonymous function return a value?

An anonymous method can return a value using a func delegate.

Can anonymous function return Javascript?

No, you cannot return a value from an asynchronous callback. Event callback will be called on click, but you can't assign its result.

What is anonymous function in C#?

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.

What is anonymous function explain with example?

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.


3 Answers

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();');
like image 35
Aiias Avatar answered Oct 18 '22 16:10

Aiias


Simplest workaround to date:

echo call_user_func(function () { return 'foo'; });
like image 143
deceze Avatar answered Oct 18 '22 17:10

deceze


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!

like image 23
David Farrell Avatar answered Oct 18 '22 15:10

David Farrell