|P is for Practical|
(dchetlin - static vs. closure) RE(3): lexical weirdness(?)by dchetlin (Friar)
|on Oct 26, 2000 at 07:42 UTC||Need Help??|
There are at least two major differences between closures and static-via-my-if-0 variables.
Here's the first. Look closely at this code and see if you can figure out what its output will be before you run it.
I'll use tilly's spoiler trick:
In other words, the closure is the same variable through all calls, including recursive ones, while each recursive call to the func_w_static gets its own copy.
The cool thing about that, of course, is that if you call func_w_closure again, you see that each recursive instance keeps its value!
The other difference is that closures can be shared among functions -- if you stick another function into the lexical scope of `$static' above, some_function and your new function will refer to the same variable.