Tag reference
@var
@var is the most flexible of the type tags: it documents the type of a
property, a class constant, or — written inline, right above an assignment
— a local variable at the point it is declared or reassigned. That last
form is especially useful for narrowing a variable’s type where PHP or a
static analyzer would otherwise infer something too broad, such as
right after decoding JSON or pulling a value out of an untyped array.
Grammar
"@phan-var" <Type> [ <Variable> ] [ <Description> ]"@phpstan-var" <Type> [ <Variable> ] [ <Description> ]"@psalm-var" <Type> [ <Variable> ] [ <Description> ]"@var" <Type> [ <Variable> ] [ <Description> ]Examples
class Model{ /** @var list<string> */ private array $tags;}/** @var User $user */$user = $session->get('user');/** @var User $user */[$user, $token] = $authenticator ->resolve();Names by platform
- phan
@phan-var- phpstan
@phpstan-var- psalm
@psalm-var- standard
@var
The variable name is optional and mainly exists to disambiguate which of several variables mentioned on the same line the type applies to; on a property or constant, where there is only ever one target, it is normally left out entirely.
Parsing a @var tag produces a VarTag instance exposing:
$type— the documentedTypeNode.$variable— the name after the type, without the leading$, ornullwhen none was written. Unlike @param, whose$variableis always present,@var’s is nullable because the name is optional in the grammar.
final class VarTag extends TypedTag{ public function __construct( string $name, TypeReference $statement, public readonly ?string $variable = null, ?DescriptionInterface $description = null, ) { parent::__construct( $name, $statement, $description, ); }}Defined by the PSR-19 draft (FIG proposed PHPDoc Tags, §5.18) and by phpDocumentor.