Class-Variable injection


In short: Why does this work:

    /* @var ArbitraryClassName $wonderfulVariableName */
    $wonderfulVariableName = $this->classVariable;

But this won't?

    /* @var ArbitraryClassName $this->classVariable */
    $this->classVariable = $externalObject;    // externalObject is of type ArbitraryClassName

Both in case of trying to get autocomplete with:

    $this->[autocomplete] or $wonderfulVariableName->[autocomplete]

The object of $this->classVariable can change and as such a declaration at the top won't be sufficient. 

A plugin-name, which adds this functionality to PHPStorm would be great too, if there're no settings or syntax for this.

Thank you very much :),



Comment actions Permalink

Hi there,

>In short: Why does this work:

>But this won't?

Because .. PHPDoc does not work this way -- you can type hint only "direct" elements

If you need to type hint $this->classVariable .. then do it inside the actual class where it is declared:

/** @var SomeClass */
protected $classVariable;

or via @property at PHPDoc comment for the class:

 * @property SomeClass $myProperty
class SomeAnotherClass {



Please sign in to leave a comment.