use
PHP-W1039One or more variables mentioned in the closure's use
clause are unused.
It is recommended to remove them from the use
clause.
A closure (also known as an "anonymous function") is a lambda that captures variables from or closes over the environment in which it was defined. In general, a closure can use variables defined in its parent scope(s).
Variables from the parent scope are not accessible from within the closure unless explicitly specified using the use
clause of the closure.
$message = 'Hi';
$defaultName = 'User';
$greet = function ($name) use ($message, $defaultName) { // $defaultName is never used
echo $message . ' ' . $name;
};
$name = 'John';
$greet($name);
// Remove unused variable(s)
$message = 'Hi';
$greet = function ($name) use ($message) {
echo $message . ' ' . $name;
};
$name = 'John';
$greet($name);
// Or, make sure it is used!
$message = 'Hi';
$defaultName = 'User';
$greet = function ($name) use ($message, $defaultName) {
if ($name === '') {
echo $message . ' ' . $defaultName;
}
echo $message . ' ' . $name;
};
$name = 'John';
$greet($name);