How to tell PhpStorm which class my variable is in?

I've got very dynamic code that is impossible to analyze statically.  So I'd like to tell PhpStorm about the types of my variables, so that autocompletion works correctly.  Example code:

$article = $this->object;

I'd like to tell PhpStorm that $a is actually an instance of the Article class, so that things like $article->title can be auto-completed.

How would I go about this?

3 comments
Comment actions Permalink

Well, I could do this:

function cast_Article(Article $x) { return $x; }
$article = cast_Article($this->object);

But this seems a bit much PHP code just to tell PhpStorm what is going on...

0
Comment actions Permalink

You can use phpdoc:

$article = $this->object;

/**
* @var Article $article
*/
$article->someArticleProperty //now you can see Article's properties and methods
0
Comment actions Permalink

Very nice!  Thanks a lot!  It works really well.  Just what the doctor ordered :-)

0

Please sign in to leave a comment.