Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get intellisense in PHP/Eclipse on custom objects pulled out of array in foreach loop?

I have a collection of custom objects (Podcast) in an array.

When I use a foreach loop to iterate through this collection, I don't have code completion on the variable that contains the object pulled out of the collection (as I would in C#/VisualStudio for instance).

Is there a way to give PHP a type hint so that Eclipse knows the type of the object being pulled out of the collection so it can show me the methods on that object in intellisense?

alt text

<?php

$podcasts = new Podcasts();
echo $podcasts->getListHtml();

class Podcasts {
    private $collection = array();

    function __construct() {
        $this->collection[] = new Podcast('This is the first one');
        $this->collection[] = new Podcast('This is the second one');
        $this->collection[] = new Podcast('This is the third one');
    }

    public function getListHtml() {
        $r = '';
        if(count($this->collection) > 0) {
            $r .= '<ul>';
            foreach($this->collection as $podcast) {
                $r .= '<li>' . $podcast->getTitle() . '</li>';
            }
            $r .= '</ul>';
        }       
        return $r;
    }
}

class Podcast {

    private $title;

    public function getTitle() { return $this->title; }
    public function setTitle($value) {  $this->title = $value;}

    function __construct($title) {
        $this->title = $title;
    }

}

?>

Addendum

Thanks, Fanis, I updated my FOREACH template to include that line automatically:

if(count(${lines}) > 0) {
    foreach(${lines} as ${line}) {
        /* @var $$${var} ${Type} */

    }
}

alt text

like image 269
Edward Tanguay Avatar asked Sep 18 '10 15:09

Edward Tanguay


1 Answers

Yes, try:

foreach($this->collection as $podcast) {
    /* @var $podcast Podcast */
    $r .= '<li>' . $podcast->getTitle() . '</
}

It's been a while since I used Eclipse but I recall it used to work there too.

like image 83
Fanis Hatzidakis Avatar answered Sep 26 '22 01:09

Fanis Hatzidakis