4

I have a class which is a simple extension of the ArrayObject class:

/**
 * what to put here? :)
 */
class Collection extends \ArrayObject
{
    /**
     * @param Item
     */
    public function add(Item $item)
    {
        $this->append($item);
    }
}

Is it possible to get my IDE to use code completion for the elements of my ArrayObject-extended class?

foreach ($collection as $item)
{
    $item->getPrice(); //my IDE doesn't not know the object here
}
faintsignal
  • 1,828
  • 3
  • 22
  • 30
ayeo
  • 482
  • 1
  • 4
  • 16
  • 3
    The IDE can't know the object type there because the collection can potentially hold any type of object. In netbeans I just do a `/**@var $item */` as the first line of the loop body and get code completion within the loop. To elaborate, since your collection extends \ArrayObject you can use it as an array to add items without needing to use your add method. – apokryfos Apr 05 '16 at 12:46
  • Thank you for reply. I know all that. I don't try to impose the type at language level. I'm looking way to use phpdoc to get code comlpetition. I can introduce method getItems() and add phpdoc for it (@returns Items[]). But I wonder if there is a better way to achieve that. – ayeo Apr 05 '16 at 13:09
  • 1
    Besides overriding the iterator functions, I don't think there is a better way. – apokryfos Apr 05 '16 at 13:13

0 Answers0