Skip to content

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()
callable
params

resultTypeLang\Type\Callable\CallableTypeNode

One parameter and a return type.

foo(T): void
callable
params
type
type

resultTypeLang\Type\Callable\CallableTypeNode

A callable as a parameter of another one.

a(int<0, max>, c(?C): mixed): void
callable
params
type
args
literal
type
callable
params
nullable
type
type
type

resultTypeLang\Type\Callable\CallableTypeNode

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
callable
params
nullable
type

resultTypeLang\Type\Callable\CallableTypeNode

Returns a union.

foo(): (A|B)
callable
params
union
type
type

resultTypeLang\Type\Callable\CallableTypeNode

A union of a callable returning A and the type B, the same as (foo(): A)|B.

foo(): A|B
union
callable
params
type
type

resultTypeLang\Type\LogicalTypeNode\UnionTypeNode

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)
callable
params
param
type
variable

resultTypeLang\Type\Callable\CallableTypeNode

Named and anonymous parameters together.

foo(A $a, B, C)
callable
params
param
type
variable
type
type

resultTypeLang\Type\Callable\CallableTypeNode

A named parameter without a type.

foo($name)
callable
params
variable

resultTypeLang\Type\Callable\CallableTypeNode

A trailing comma.

foo(A $a, B $b,)
callable
params
param
type
variable
param
type
variable

resultTypeLang\Type\Callable\CallableTypeNode

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&)
callable
params
param
type

resultTypeLang\Type\Callable\CallableTypeNode

foo(T &$name)
callable
params
param
type
variable

resultTypeLang\Type\Callable\CallableTypeNode

An output parameter without a type.

foo(&$name)
callable
params
param
variable

resultTypeLang\Type\Callable\CallableTypeNode

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=)
callable
params
param
type
default

resultTypeLang\Type\Callable\CallableTypeNode

An optional output parameter with a name.

foo(T &$name=)
callable
params
param
type
variable
default

resultTypeLang\Type\Callable\CallableTypeNode

Default values.

foo(int $a = 42, string $b = 'text', ?Foo $c = null, int $d = Foo::BAR)
callable
params
param
type
variable
default
literal
param
type
variable
default
literal
param
nullable
type
variable
default
literal
param
type
variable
default
const

resultTypeLang\Type\Callable\CallableTypeNode

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...)
callable
params
param
type

resultTypeLang\Type\Callable\CallableTypeNode

A variadic output parameter with a name.

foo(T &...$name)
callable
params
param
type
variable

resultTypeLang\Type\Callable\CallableTypeNode

A variadic parameter without a type.

foo(...$name)
callable
params
param
variable

resultTypeLang\Type\Callable\CallableTypeNode

The ellipsis before the type.

foo(...T)
callable
params
param
type

resultTypeLang\Type\Callable\CallableTypeNode