Doc comments for closures in an array? Follow
Hi,
let’s imagine I have an array of Closures, such as this:
$closure = array(
function() { /* do something */ },
function() { /* do something else */ },
function() { /* do yet another thing */ },
);
Now, I want to loop over the array:
foreach ($closure as $c) {
$c();
}
PhpStorm will mark the $c() as a warning (“Function name must be a string, Closure or …”). How can I make PhpStorm recognize that the array DOES contain closures? I tried something like …
/**
* @var Closure[]
*/
$closure = array(
…, but without success.
Carsten
Please sign in to leave a comment.
Hi Carsten,
You have incorrect (to be precise, incomplete) syntax for @var PHPDoc for local variable. This syntax is correct for class variable, but for local variable you have to include variable name as well, e.g:
or
Another possible approach is to provide type hint (same way as above) but for $c variable, e.g.
Thanks, that solved the problem.
Carsten