This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.
Summary: | Variables in closures are scoped to the containing scope | ||
---|---|---|---|
Product: | php | Reporter: | dharkness <dharkness> |
Component: | Editor | Assignee: | Ondrej Brejla <obrejla> |
Status: | VERIFIED FIXED | ||
Severity: | normal | CC: | vriha |
Priority: | P3 | ||
Version: | 7.2 | ||
Hardware: | All | ||
OS: | All | ||
Issue Type: | DEFECT | Exception Reporter: |
Description
dharkness
2012-07-26 18:55:02 UTC
It's not so simple, because scope of that $index var is not just the body of closure, but the body of enclosing function too. Because that variable is declared in outer scope, so "usage" of it is that usage as a closure param. I think that ve are not able to handle these both cases, since it's still the one physical variable. And it's primary scope is outer scope...scope of it's declaration. So for me, it works as intended (it was designed to work this way). But I will let it opened as a reminder. Thanks for reporting. Variables declared immediately after the "function" keyword are local to the closure. Their values are provided when the closure is called. They function exactly as declared parameters for regular functions. function foo() { $bar = 'bar'; $closure = function ($bar) { var_dump($bar); $bar = 'bye'; }; $closure('hi'); var_dump($bar); } foo(); Output: string(2) "hi" string(3) "bar" However, variables declared in the optional "use" clause *are* the same variables from the enclosing scope. The "use" block allows you to pass variables from the enclosing scope to the closure. This is why you must declare a local variable such as "$self" so you can pass $this to the closure; the closure doesn't have access to $this. function foo() { $bar = 'bar'; $closure = function () use (&$bar) { var_dump($bar); $bar = 'bye'; }; $closure(); var_dump($bar); } foo(); Output: string(3) "bar" string(3) "bye" Oooouuuu...yes, you are definitelly right. My bad. Sorry. Thanks. Fixed in web-main #a3e23bd95b9f Integrated into 'main-golden', will be available in build *201207280002* on http://bits.netbeans.org/dev/nightly/ (upload may still be in progress) Changeset: http://hg.netbeans.org/main-golden/rev/a3e23bd95b9f User: Ondrej Brejla <obrejla@netbeans.org> Log: #216027 - Variables in closures are scoped to the containing scope Vlado, please, can you verify that for 7.2 patch 1? Thanks in advance. verified Product Version: NetBeans IDE Dev (Build 201208080001) Java: 1.7.0_06; Java HotSpot(TM) Client VM 23.2-b09 System: Linux version 3.0.0-23-generic running on i386; UTF-8; en_US (nb) Transplanted into releases, please verify, thanks. Integrated into 'releases', will be available in build *201209010822* or newer. Wait for official and publicly available build. Changeset: http://hg.netbeans.org/releases/rev/edaf720e6bf7 User: Ondrej Brejla <obrejla@netbeans.org> Log: #216027 - Variables in closures are scoped to the containing scope (transplanted from a3e23bd95b9f86edca04a8098acea68c4d13f6a2) verified in patch Product Version: NetBeans IDE 7.2 (Build 201207171143) Java: 1.7.0_06; Java HotSpot(TM) Client VM 23.2-b09 System: Linux version 3.2.0-29-generic-pae running on i386; UTF-8; en_US (nb) |