The use syntax is a little weird at first but it turns out to be straightforward to understand. In fact, being explicit about what is being closed over makes the function itself easier to understand.
PHP, being the quirky language that it is, needs the use syntax to enable closures for cases where there are dynamic variable references. This is a bad example but illustrates the problem:
PHP, being the quirky language that it is, needs the use syntax to enable closures for cases where there are dynamic variable references. This is a bad example but illustrates the problem:
$foo = 'bar'; $fun = function() use ($foo) { $var = 'foo'; echo ${$var}; }; $fun(); // output 'bar'
$fun = function() { $var = 'foo'; echo ${$var}; }; $fun(); // fails
Sure, you could close over the entire local namespace but that's an efficiency nightmare and leads to unpredictable code.