I know what the use operator is doing in something like function($x,$y) use ($z) { ...
What I don't understand is why PHP uses this construction when other languages don't?
I know what the use operator is doing in something like function($x,$y) use ($z) { ...
What I don't understand is why PHP uses this construction when other languages don't?
Javascript has rather loose variable scoping (you don't need to declare variables as global). PHP has tighter variable scoping (if a variable isn't defined within the scope that it's used, and isn't brought in with global
, then it doesn't exist).
The use
declaration tells PHP to make those variables available within the closure (and likely also tells the garbage collector not to clean them up until after the closure gets cleaned up).