I’ve encountered this code, but I can’t understand its logic. Could someone explain why the output is the way it is?
<?php
$functions = [
function($next) {
echo 'A' .PHP_EOL;
$next();
},
function($next) {
echo 'B' .PHP_EOL;
$next();
},
function($next) {
echo 'C' .PHP_EOL;
$next();
}
];
$a = function() {
echo 'Main Content' .PHP_EOL;
};
foreach($functions as $function) {
$a = fn() => $function($a);
}
$a();
Output:
C
B
A
Main Content
2
Answers
It’s a kind of reversed recursion, the code snippet is setting an initial callback function
line 18
and then looping over the$functions
array, overriding the old variable$a
by the new function in the loop. At the end, the last function (the one withecho 'C' .PHP_EOL;
) is being called, causing an initial call to the$next()
callbackline 14
, which is – from the foreach loop – points to theb
function, which is being called, causing the invocation of the$next()
callbackline 10
and so on. Until it reaches the initial function declaration.The code you provided looks like from some middleware, wherein the outer function must utilize the result of inner function to perform some task
For simplicity sake I have converted the 3 iterations of the foreach loop into an equivalent code
Now when the loop starts, on its 1st iteration it will be something like
(For better clarity I used the "full-form" of anonymous function instead of the shorthand notation)
$firstLoopFn
becomes the new$a
in your original codeWhat happens in the 2nd iteration? Same
$secondLoopFn
becomes the new$a
in your original codeSimilarly the 3rd iteration
$thirdLoopFn
becomes the new$a
in your original codeNow the order, it is nothing but similar to this
Function3rd
will run first the thenFunction2d
and so onPlease observe in your case the last iteration of foreach loop adds the final "layer" so it gets runs first (Function3rd here)
Also the fact that
echo
precedes the call to inner function in the source code so it gets run before the inner function called, to simply say function prints first then calls the inner function