PHP Closures scoping of variables -
i looking @ php example of closures on http://us1.php.net/manual/en/functions.anonymous.php
it provides example code below , states:
closures may inherit variables parent scope. such variables must declared in function header. inheriting variables parent scope not same using global variables. global variables exist in global scope, same no matter function executing. parent scope of closure function in closure declared (not function called from). see following example:
i confused how working though. $quantity , $product variables not seem me available inside closure function. wouldn't parent scope 1 scope in case gettotal() function?

you're misunderstanding function signature. $quantity , $product regular arguments passed function when it's called, indeed not exist in parent scope , aren't meant to. use ($tax, &$total) closed over variables parent scope.
$foo = 'foo'; // closed on variable // vvvv $func = function ($bar) use ($foo) { // ^^^^ // regular function argument return $foo . $bar; }; echo $func('baz'); // "foobaz"
Comments
Post a Comment