Suggestions for iterators
Hello,
I am looking for a way to specify type of items in collection for PhpStorm.
For example, i have class which implements interface Iterator.
Inside this class I have an array of items
class Collection implements Iterator, Countable {
protected $key = 0;
/**
* @var EntityAbstract[]
*/
protected $array = [];
public function current() {
return $this->array[$this->key];
}
... rest of necessary methods ...
}
Now, I want to iterate through my collection with, let's say, foreach:
$co = new Collection();
foreach ($co as $item) {
$item->...
}
Here I do not have suggestions for $item, because IDE knows nothing about type of items in my collection, I have specified type for property $array, but IDE does not know that $array is my storage for collection.
I know, that I can specify type of my collection items right in foreach, like this
foreach ($co as $val) {
/** @var $val EntityAbstract */
prr($val->...);
}
But I need to do it all of the time and it is not convenient. I wonder, maybe there is some way to specify type of collection items in one place, because it is quite logical as I think.
请先登录再写评论。
Hi there,
Currently it's not possible to define it once in the actual class, AFAIK.
The best what you can do right now is to have it declared when instantiating such class (which possibly can be the only instance, depending of what this class does and how it is used), i.e.
Thank you for response, as usual =)
This solution interesting but there is no big big difference where to specify type manually.
It would be much more convenient if we could specify type of stored items in class phpDoc...