I want to do something like this:
function func($callback) {
$result = $callback(???); // $callback must be called here
//...
}
//...
func(function(['foo' => 'buu']) {
$a = func_get_arg(0);
// do something with $a and return something...
return $something;
})
It is possible in php?
I can do something like below, but this is not what I want to do:
function func($args, $callback) {
$result = $callback($args);
//...
}
func(['foo' => 'boo'], function($args) {
$a = $args; // etc.
})
I use anonymous function this ways :
$mySuperFunction = function($arg)
{
echo ("Hello " . $arg);
};
function AnotherFunction($args)
{
echo ("Another hello " . $args);
}
function WrappingAnonymous($callback, $args)
{
$callback($args);
}
function WrappingAnonymousWithoutArgs($callback)
{
$callback();
}
WrappingAnonymous($mySuperFunction, "World");
WrappingAnonymous("AnotherFunction", "World");
WrappingAnonymous(function($someArgs)
{
echo "Yet another Hello " . $someArgs;
}, "World");
WrappingAnonymousWithoutArgs(function($someArgs = "World")
{
echo "Now, a 4th other Hello " . $someArgs;
});
Outputs :
Hello World
Another hello World
Yet another Hello World
Now, a 4th other Hello World