10

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

1
  • Good stuff :) Good usage of templates too. Commented Nov 6, 2010 at 8:01

2 Answers 2

19

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.

Sign up to request clarification or add additional context in comments.

1 Comment

You're welcome! Just fyi, some IDEs may require a proper docblock, ie double asterisks: /** @var ... */
0

This might help some other people from the internet looking for a more succint solution to this problem.

My solution requieres PHP7 or higher. The idea is to map the array with an anonymous function and take advantage of type-hinting.

  $podcasts = getPodcasts();
  $listItems = array_map(function (Podcast $podcast) {
      return "<li>" . $podcast->getTitle() . "</li>";
  }, $podcasts);
  $podcastsHtml = "<ul>\n" . implode("\n", $listItems) . "\n</ul>";

In most cases a foreach can be converted into an array_map, it simply takes a little bit of paradigm shift towards functional programming.

If you use Laravel (I am sure other frameworks have Collections too) you could even chain these array maps with array filters and other functional stuff like this:

$html = "<ul>" . collect($podcasts)
  ->filter(function (Podcast $p) { return $p !== null; }) // filtering example
  ->map(function (Podcast $p) { return "<li>".$p->getTitle()."</li>"; }) // mapping
  ->implode("\n") . "</ul>";

In plain php chaining those array functions looks pretty uggly...

But there you go! a native way of type hinting your array iterations.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.