Class error with foreach?

If I define a class thus:

class item {
// some variables go here

public function add() {
// some code goes here
return $result;
}
}

$things = new item;
foreach($things as $thing) {
$retval = $thing->add();
}

I find that the IDE doesn't seem to see add as a method of the item class but reports it as unkown.

Am I doing this wrong or is it a bug in PHPStorm?

0
1 comment

Hi there,

Because $thing is of unknown type here (perhaps it's a bad code sample, bit IDE is correct here).

You may typehint $thing via PHPDoc, then it will work. E.g.

/** @var item $thing */
0

Please sign in to leave a comment.