Syntax
Callables
A callable type describes a function: a name followed by a list of parameters in parentheses and, optionally, a return type. A callable may also declare template parameters, see Generic callables.
No parameters, no return type.
foo()
resultTypeLang\Type\Callable\CallableTypeNode
One parameter and a return type.
foo(T): void
resultTypeLang\Type\Callable\CallableTypeNode
A callable as a parameter of another one.
a(int<0, max>, c(?C): mixed): void
resultTypeLang\Type\Callable\CallableTypeNode
Return type
Section titled “Return type”The return type is written after a colon (:). A nullable type (?T) and a
list (T[]) belong to the return type. A union (|), an intersection (&)
or a condition behind it belongs to the type around the callable, the way
PHPStan, Psalm and Phan read it: to return such a type, wrap it in
parentheses.
Returns a nullable type.
foo(): ?A
resultTypeLang\Type\Callable\CallableTypeNode
Returns a union.
foo(): (A|B)
resultTypeLang\Type\Callable\CallableTypeNode
A union of a callable returning A and the type B, the same as
(foo(): A)|B.
foo(): A|B
resultTypeLang\Type\LogicalTypeNode\UnionTypeNode
Named parameters
Section titled “Named parameters”A name after the type of a parameter, beginning with $, is the name it
is passed by.
The type of a named parameter may be omitted. A trailing comma after the last
parameter is allowed.
foo(T $name)
resultTypeLang\Type\Callable\CallableTypeNode
Named and anonymous parameters together.
foo(A $a, B, C)
resultTypeLang\Type\Callable\CallableTypeNode
A named parameter without a type.
foo($name)
resultTypeLang\Type\Callable\CallableTypeNode
A trailing comma.
foo(A $a, B $b,)
resultTypeLang\Type\Callable\CallableTypeNode
A parameter cannot be empty.
foo(A, , B)
errorMalformedSyntaxException Invalid callable parameters, a parameter, "," or ")" is expected
Output parameters
Section titled “Output parameters”A parameter passed by reference, which the function may change, is marked
with an ampersand (&) after the type and before the name. A parameter
without a type begins with the ampersand.
foo(T&)
resultTypeLang\Type\Callable\CallableTypeNode
foo(T &$name)
resultTypeLang\Type\Callable\CallableTypeNode
An output parameter without a type.
foo(&$name)
resultTypeLang\Type\Callable\CallableTypeNode
The ampersand stands after the type.
foo(&T)
errorMalformedSyntaxException Invalid callable parameters, a parameter, "," or ")" is expected
Optional parameters
Section titled “Optional parameters”A parameter the caller may omit ends with an equals sign (=). The sign may
be followed by a default value: a literal (boolean or null,
string, number) or a
constant (FOO, Foo::BAR, Foo::class).
Masks, lists and other types are not default values.
foo(T=)
resultTypeLang\Type\Callable\CallableTypeNode
An optional output parameter with a name.
foo(T &$name=)
resultTypeLang\Type\Callable\CallableTypeNode
Default values.
foo(int $a = 42, string $b = 'text', ?Foo $c = null, int $d = Foo::BAR)
resultTypeLang\Type\Callable\CallableTypeNode
The equals sign stands at the end.
foo(T= $name)
errorMalformedSyntaxException Invalid callable parameters, a parameter, "," or ")" is expected
Variadic parameters
Section titled “Variadic parameters”A variadic parameter is marked with an ellipsis (...) after the type. With
both markers, the ampersand comes first. The ellipsis may also stand before
the type (...T), the way some phpdoc dialects write it, but then the
parameter has no name.
foo(T...)
resultTypeLang\Type\Callable\CallableTypeNode
A variadic output parameter with a name.
foo(T &...$name)
resultTypeLang\Type\Callable\CallableTypeNode
A variadic parameter without a type.
foo(...$name)
resultTypeLang\Type\Callable\CallableTypeNode
The ellipsis before the type.
foo(...T)
resultTypeLang\Type\Callable\CallableTypeNode
An ellipsis before the type is not followed by a name.
foo(...T $name)
errorMalformedSyntaxException Invalid callable parameters, a parameter, "," or ")" is expected
The ampersand comes before the ellipsis.
foo(T ...&$name)
errorMalformedSyntaxException Invalid callable parameters, a parameter, "," or ")" is expected
A variadic parameter cannot be optional.
foo(T ...$name=)
errorMalformedSyntaxException Invalid callable parameters, a parameter, "," or ")" is expected