2

Is it in PHP possible to pass a executed function as function's parameter, so that the result of the function will be hand over but not a callback function object?
Without touching the scope function (no eval or extra magic execution).

Let's say, the parameter I want to give the function is not clear. So there must be a decision for that before given to the function.
Currently this always lookes like this:

$tmp = ($something == $bla)?42:7;
myFunction($tmp);

For such a short decision it is not so problematic. But sometimes there are more then two possibilities. So a if-elseif is needed, or a switch-case.
But it would be so much cleaner to just put somthing like this:

myFunction(function(){
    if(...){ return 42; }
    elseif(...){ return 7; }
    elseif(...){ return 5; }
    elseif(...){ return 12; }
    else{ return 1; }
});

So without saving it first to a variable.
Just for cleaner code, less $tmp or $trash variables.

4
  • 1
    "The output will be 42 in the console" - No, it won't Commented Dec 5, 2019 at 8:27
  • If something is that complex, shouldn't it be a named function? Commented Dec 5, 2019 at 8:37
  • PHP has exactly the same syntax. Commented Dec 5, 2019 at 8:49
  • 1
    At some point you'll have to actively call the function. Either before passing it to the other function (effectively passing only the return value), or inside the other function. Commented Dec 5, 2019 at 8:51

2 Answers 2

2

Anonymous functions and variable functions should be what you are looking for:

Anonymous functions, also known as closures, allow the creation of functions which have no specified name. They are most useful as the value of callback parameters, but they have many other uses.

Anonymous functions are implemented using the Closure class

And a code example:

<?php
$greet = function($name)
{
    printf("Hello %s\r\n", $name);
};

$greet('World');
$greet('PHP');
?>

And one more code example, just for the sake of completeness, about how to pass a function name as parameter, and call it aftwerwards:

<?php

function here() {
  print 'here';
}


function dynamo($name) {
 $name();
}

//Will work
dynamo('here');
//Will fail
dynamo('not_here');

Last example was extracted from here

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

2 Comments

You didn't really show anonymous functions. You still gave them names.
The first example assigns a function to a variable, yes. But I think that is the easiest example to read that I found in the php docs. There are a few examples in there with fully anonymous functions in case anyone is interested.
2

So with the understanding of anonymous functions (Thanks Lucas Meine ), I found that you can execute a function directly by surrounding it with brackets, followed by parameter brackets that will be empty or not depending on whether you want to give the lower level function some of your variables out of the main context.

In all following examples the output of var_dump is 42.

example without variable parameters

// the function that should later be called
function myFunction($test){
    var_dump($test);
}

// function call with a executed function as parameter
myFunction((function(){
    return 42;
})());

example with variable parameters

$someVar = 42;

function myFunction($test){
    var_dump($test);
}

myFunction((function($tmp){
    return $tmp;
})($someVar));

or with the use statement

$someVar = 42;

function myFunction($test){
    var_dump($test);
}

myFunction((function() use ($someVar){
    return $someVar;
})());

Comments

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.