Sometimes when you initialize variables, you want to pass them too complex values ββto calculate in one command, so you usually either evaluate the dummy variable before, and then pass its value or define a function in another place and pass it, return the value of our variable.
My question (desire): is it possible to compute a variable on the fly using anonymous functions?
for example, instead:
$post = get_post();
$id = $post->ID;
$array = array(
'foo' => 'hi!',
'bar' => $id
);
Let's use something like this:
$array = array(
'foo' => 'hi!',
'bar' => (function(){
$post = get_post();
return $post->ID;
})
);
The code is summary random.
source
share