Syntax
Generics
Each type can take arguments containing other type declarations. Validation of arguments and their number lies on the implementing side; the parser does not limit their number or nesting and does not check their semantic correctness in any way.
Each argument is enclosed in triangle brackets < and > and separated by a
comma (,).
Reference to an existing class, interface or enum with template arguments.
Path\To\ExampleClass<T, U>
resultTypeLang\Type\NamedTypeNode
Missing template argument.
example<>
errorMalformedSyntaxException A template argument is expected after "<"
Leading comma is NOT allowed.
example<,T>
errorMalformedSyntaxException A template argument is expected after "<"
Call-site hints
Section titled “Call-site hints”Each template argument allows you to define an additional hint, which can be used, for example, in static analyzers to indicate the call-site variance.
The hint is one of the words covariant, contravariant or invariant
written in lower case before the type of the argument.
The covariant hint before the template argument`s type.
HashMap<array-key, covariant Request>
resultTypeLang\Type\NamedTypeNode
Hints of different arguments are independent of each other.
Handler<contravariant Request, invariant Response>
resultTypeLang\Type\NamedTypeNode
The hint applies to the whole type that follows it.
Collection<covariant Foo|Bar>
resultTypeLang\Type\NamedTypeNode
Only the three hint words are allowed.
Collection<out User>
errorMalformedSyntaxException Invalid template arguments, "," or ">" is expected
Multiple hints are not allowed.
HashMap<array-key, covariant covariant Request>
errorMalformedSyntaxException Invalid template arguments, "," or ">" is expected
Wildcard arguments
Section titled “Wildcard arguments”A template argument may be left unsaid, written as an asterisk (*) in the
place of the type. A wildcard argument does not accept a hint.
Any argument at all.
Collection<*>
resultTypeLang\Type\NamedTypeNode
A wildcard beside a type.
HashMap<array-key, *>
resultTypeLang\Type\NamedTypeNode
Several wildcards.
HashMap<*, *>
resultTypeLang\Type\NamedTypeNode
An asterisk is a template argument alone and is not a type of its own.
int|*
errorUnexpectedTokenException Unexpected "*", expected a type name, an identifier, "true", "false" and other
A wildcard cannot carry a hint.
Collection<covariant *>
errorMalformedSyntaxException Invalid template arguments, "," or ">" is expected
List syntax
Section titled “List syntax”In addition to modern list declarations such as list<int> or
array<array-key, int>, the legacy int[] syntax is allowed.
List (array) of User objects.
User[]
resultTypeLang\Type\WrappingTypeNode\TypesListTypeNode
List of list (nested array) of User objects.
User[][]
resultTypeLang\Type\WrappingTypeNode\TypesListTypeNode
Square brackets with a type inside are not a list, but an
offset access: User[int] is the type of the element of
User at the int key (was used in the PSR by mistake as a list).
User[int]
resultTypeLang\Type\TypeOffsetAccessTypeNode