You must explicitly declare variables inherited from the parent scope with the use keyword:
// use the `$dir` variable from the parent scope function ($item) use ($dir) {
function scandir_only_files($dir) { return array_filter(scandir($dir), function ($item) use ($dir) { return is_file($dir.DIRECTORY_SEPARATOR.$item); }); }
See this example on the anonymous functions page.
Closing can inherit variables from the parent scope. Any such variables must be declared in the function header. The parent region of the closure is the function in which the closure was declared (not necessarily the function from which it was called).
source share