15

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

7
  • Do you really need an anonymous function? Sounds like you need a regular one and just call it inside the loop Commented Apr 7, 2013 at 22:40
  • Depends on what php version you have... I believe php 5.4 might be able to do this, but servers running older packages (i.e. Debian Squeeze I know runs php 5.3) won't. Commented Apr 7, 2013 at 22:41
  • @JulianH.Lam my examples were tested on PHP 5.4.13 Commented Apr 7, 2013 at 22:42
  • @JulianH.Lam Anonymous functions are available since PHP 5.3 Commented Apr 7, 2013 at 22:43
  • I've established this isn't possible so, feature request can be found here: bugs.php.net/bug.php?id=64608 Commented Apr 7, 2013 at 23:10

4 Answers 4

25

Simplest workaround to date:

echo call_user_func(function () { return 'foo'; });
Sign up to request clarification or add additional context in comments.

1 Comment

anonymous function dereferencing is now available in PHP7!
2

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

4 Comments

I believe OP is looking for the value to be returned when accessing $myFunc, not $myFunc().
@TimCooper No, you need the function calling, see here codepad.viper-7.com/3nnjGW
@Sam: Yes, I am aware you have to evaluate the function to get the return value, but I believe this is a question on where you do the evaluating. I think OP wants the anonymous function to be evaluated when it's defined.
eval is about the worst solution to this.
1

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!

Comments

0

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

2 Comments

In my case I need the return straight off the bat, Running $a = function(){ return mt_rand(); }(); doesn't work.
Because that is an assignament, you can do $a(), what's the problem?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.