Say I have a class Book
which has a property on it called $chapters
. In the implementation I'm working with, this property is represented by a generic helper class called Collection
, which in turn contains an array of Chapter
classes.
Example:
class Book {
/** @var Collection */
public $chapters;
}
class Collection {
private $items;
public function getItems(): array {
return $this->items;
}
}
class Chapter {}
Is it possible to annotate the $chapters
property in Book
so that my IDE knows that it is a Collection
object, but that a call to that collection's getItems()
method will return an array of Chapter
instances?
Can I do this without creating a child class of Collection
and annotating that?
EDIT: I don't think I was clear in my goal. I'm looking to type hint a class which is outside of the Book
class and give guidance on what it's $items
property would be — something like this (which I'm sure is invalid):
class Book {
/**
* @var Collection {
* @property Chapter[] $items
* }
*/
public $chapters;
}