I am looking at the PHP example of Closures
on http://us1.php.net/manual/en/functions.anonymous.php
It provides the example code below and states:
Closures may also inherit variables from the parent scope. Any such variables must be declared in the function header. Inheriting variables from the parent scope is not the same as using global variables. Global variables exist in the global scope, which is the same no matter what function is executing. The parent scope of a closure is the function in which the closure was declared (not necessarily the function it was called from). See the following example:
I am confused as to how this is working though. $quantity
and $product
variables do not seem to me that they would be available inside the Closure function. Wouldn't the Parent Scope
be 1 scope up in this case the getTotal()
function?