Dennis
Dennis

Reputation: 78

Explicit typing on property of instance

To help my editor better understand my code, I sometimes need to add a comment like this:

/* @var $container Container */

This work good, but sometimes I would need something like this:

/* @var $this->container Container */

Is there something like this?

Upvotes: 0

Views: 88

Answers (1)

LazyOne
LazyOne

Reputation: 165088

A few things first:

1. PHPDoc comments start with /**.

For compatibility reasons PhpStorm understands PHPDoc tags in ordinary /* comments as well, but you better use the correct symbols for them.

2. The correct order of elements for inline @var tag can be seen here:
https://github.com/phpDocumentor/fig-standards/blob/master/proposed/phpdoc-tags.md#517-var

/** @var [type] [element_name] [<optional description>] */

e.g.

/** @var Container $container */

Just as with #1: PhpStorm understands such comments even if elements are swapped around (for compatibility with other (old) IDEs / old code).


Your actual question:

Inline @var allows typehinting only local / ordinary variables. You CANNOT use it for compound variables (cannot use $this->container or $someObject->someVar here).

This is wrong:

/* @var $this->container Container */

// even if it uses correct order/style
/** @var Container $this->container */

If anything: such typehints should be provided in the actual class, above the actual property declaration (where you omit the [element_name] part) https://github.com/phpDocumentor/fig-standards/blob/master/proposed/phpdoc-tags.md#examples-15

class MyAwesomeClass
{
    /** @var Container Optional description here */
    protected $container;
    ...
}

Upvotes: 1

Related Questions