Conceptually, $foo
is like a function with 2 inputs, x and y. Calling $foo
is like a partial function evaluation that sets x=5. The $bar
call then evaluates the function with y=4. So you end up with x * z + y = 5 * 11 + 4 = 59.
To put it another way, $foo
is a lambda that evaluates to another lambda. So $bar
becomes a lambda that evaluates to a number.
$z = 11
$foo = $x ==> $y ==> $x * $z + $y;
// foo = f(x,y,z) = x * z + y
$bar = $foo(5); // $bar is now $y ==> 5 * $z + $y
// bar = f(y,z) = 5 * z + y
$result = $bar(4); // this gives you 5 * $z + 4 = 59
// result = f(z) = 5 * z + 4
var_dump(result); // outputs 59