Jordan Doyle
Jordan Doyle

Reputation: 3026

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

Upvotes: 15

Views: 12544

Answers (4)

David Farrell
David Farrell

Reputation: 3722

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!

Upvotes: 1

Aiias
Aiias

Reputation: 4748

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();');

Upvotes: 2

deceze
deceze

Reputation: 522412

Simplest workaround to date:

echo call_user_func(function () { return 'foo'; });

Upvotes: 25

Sam
Sam

Reputation: 2970

You must assign the function to a var, look here

This work

    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();
    }
];
}
echo $array[5]['new']();

or

$function = function(){
 // again, we'll just use mt_rand
 return mt_rand();
};

echo $function();

Upvotes: 0

Related Questions