Julian
Julian

Reputation: 3859

Declare an object as a specific class in php

Is there an opportunity, that will allow it to declare a variable in php as a specific object, so that IDEs like Netbeans can detect that Object and suggest me the possible methods and variable in that class?

What I know, is that it works with function like this example:

public static function add(\Event $element) {
    //The $element variable is now declared, that it have to be an Event object
}

In the theory, my question looks like this:

\Event $events = EventContainer::getAll();

But unfortunately that wouldn't work.

Upvotes: 1

Views: 85

Answers (1)

Tschallacka
Tschallacka

Reputation: 28722

In eclipse I usually use this pattern:

class Foo {
  /**
   * @var $barArray \Baz\Bar[]
   */
  protected $barArray;

  /**
   * @var $bar \Baz\Bar
   */
  protected $bar

  /**
   * @return \Baz\Bar
   */
  public function getBar() 
  {
      return $this->bar;
  }

  /**
   * @return \Baz\Bar[]
   */
  public function getAllBar() 
  {
      return $this->barArray;
  }
}

That way eclipse knows by the javadoc what to use in autocomplete. It might work the same in other editors.

Upvotes: 3

Related Questions