Search code examples
phpintellij-ideaphpstormphpdoc

Annotate a class member that doesn't exist (virtual property)


Is there a way to have an annotation for a class member that isn't declared explicitly? Like

class Foo
{
    /** @var string $bar */
    // public $bar; // <--- without this line

    ...other stuff...
}

Background: I use __get to provide virtual properties for my class and want these properties to appear in the phpstorm/idea autocomplete box.


Solution

  • Use the @property* annotation on the class.

    /**
     * @property string $bar
     */
    class Foo {}
    

    Screenshot of PHPStorm's autocomplete box demonstrating.


    *or @property-read or @property-write as appropriate.