PHPDoc type hinting for array of objects Follow
{ return key($this->_elements); } public function next() { return next($this->_elements); } public function current() { return current($this->_elements); }
// core class
class ArrayCollection extends Countable, IteratorAggregate, ArrayAcces {
/* @var array */
// array of objects
private $_elements;
public function key()
}
class B { }
class A
private $items;
/**
* @return B[]
*/
public function getItems() {
// say $this->items already contains an ArrayCollection class instance with an array of B class objects inside. return $this->items; }
}
it works okay. However i have not just an array of objects of type B, i have an ArrayCollection object which implements ArrayAccess, which contains an array of objects of type B.
So $items = $a->getItems();
$items[0]->somemethod() suggest correctly
but
$items->
does not suggest anything.
Please sign in to leave a comment.
Hi Alexei,
Could you please clarify your example a bit more (more complete example code) ?
Your code hint clearly states -- "array of objects of type/class B" -- it does not say that it is a collection itself. Most likely you will need something like this:
In any case -- these tickets can be related to your case:
This looks like a workaround because it does not actually return B[] OR ArrayCollection, it returns both, but it helped! ;) Hope jetbrains team thinks of a more correct solution for this.
I updated the example for the sake of correctness, i took it out from context, it is from Doctrine2.
Thank you very much!
Well .. PhpStorm uses PHPDoc syntax which (as far as I know) has no special way of describing "class A AND class B" therefore @return ArrayCollection|B[] seems pretty legit and makes sense (at very least IMO).