16

Is it possible to pass an anonymous function as an argument, and have it execute immediately, thus passing the function's return value?

function myFunction(Array $data){
    print_r($data);
}

myFunction(function(){
    $data = array(
        'fruit'     => 'apple',
        'vegetable' => 'broccoli',
        'other'     => 'canned soup');
    return $data;
});

This throws an error due to the Array type-hint, complaining of an object being passed. Alright, if I remove the type-hint, it of course spits out Closure Object, rather than the results I want. I understand that I am technically passing an object instance of Closure to myFunction, however, I'm near certain that I've seen this accomplished elsewhere. Is this possible? If so, what am I doing wrong?

For the sake of this discussion, I cannot modify the function to which I'm passing the closure.

tl;dr: How can I pass an anonymous function declaration as an argument, resulting in the return value being passed as the argument.

PS: If not clear, the desired output is:

Array
(
    [fruit] => apple
    [vegetable] => broccoli
    [other] => canned soup
)
1
  • You've probably seen this in Javascript as (function () { })(), but PHP's functions aren't that flexible. Commented Nov 16, 2010 at 21:50

3 Answers 3

12

Recently, I was solving similar problem so I am posting my code which is working as expected:

$test_funkce = function ($value) 
{

    return ($value + 10);
};


function Volej($funkce, $hodnota)
{   
    
   return $funkce($hodnota);
   //or alternative syntax
   return call_user_func($funkce, $hodnota); 
}

echo Volej($test_funkce,10); //prints 20

Hope it helps. First, I am creating closure, then function which accepts closure and argument and is invoking its inside and returning its value. Simply enough.

PS: Answered thanks to this answers: answers

Sign up to request clarification or add additional context in comments.

2 Comments

Too bad this answer is so new compared to the other ones, but this is definitely what helped me out in my case, thanks for this!
There is few good resources for functional php but actually php behaves really strange when I am trying something with closures.
11

You can't. You'd have to call it first. And since PHP doesn't support closure de-referencing yet, you'd have to store it in a variable first:

$f = function(){
    $data = array(
        'fruit'     => 'apple',
        'vegetable' => 'broccoli',
        'other'     => 'canned soup');
    return $data;
};
myfunction($f());

2 Comments

Thanks ircmaxell; Darn, I suppose the closures passed as arguments I've seen were called at some point in the body of the function they were passed to. Too bad.
Few years later ... is possible
5

You're passing the function itself, not the results as you noticed. You'd have to execute that function immediately doing something like this:

myFunction((function() {
    return ...;
})(), $otherArgs);

PHP doesn't support such things, so you're forced to assign that function to some variable and execute it:

$func = function() { ... };
myFunction($func(), $otherArgs);

3 Comments

Really? Do we have to keep going down the road of badmouthing PHP (saying it sucks, not pointing out a deficiency) this often? So much hate... If you think it sucks, that's fine. Get over it...
Thanks Crozin; I'll have to consider other options.
@ircmaxell He clearly pointed out one of the may deficiencies why PHP sux. And yes, you cannot go down the road to badmouth PHP often enough to tell others about the fact, that PHP sux and cannot be no more considered a language of modern times.

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.