Syntax
Conditional types
A conditional (ternary) type describes a type that depends on the result of
comparing a subject with some other type. It consists of a condition,
followed by a question mark (?), the type used if the condition is true,
a colon (:), and the type used if the condition is false.
<Subject> <Operator> <Type> ? <TypeIfTrue> : <TypeIfFalse>T is int ? "int type" : "not int type"
resultTypeLang\Type\Condition\ConditionalTypeNode
Both the subject and the type being compared against can be a union
(and so any type below it: an intersection, a nullable, a list, etc.) or a
variable ($name). The branches can be any type, including another
conditional type, so the chain T is A ? B : C is D ? E : F is read as
T is A ? B : (C is D ? E : F).
A named type as the subject.
T is int ? A : B
resultTypeLang\Type\Condition\ConditionalTypeNode
A variable as the subject.
$value is int ? A : B
resultTypeLang\Type\Condition\ConditionalTypeNode
A union type as the subject.
T is A|B ? C : D
resultTypeLang\Type\Condition\ConditionalTypeNode
A callable type as the subject.
foo() is int ? A : B
resultTypeLang\Type\Condition\ConditionalTypeNode
Parentheses may be used to disambiguate or nest conditional types.
T is int ? (U is string ? A : B) : C
resultTypeLang\Type\Condition\ConditionalTypeNode
A bare question mark, without a preceding condition, is not a conditional
type: it will be parsed as the beginning of a nullable type
and fail because the placement of ? is wrong.
T ? U : V
errorUnexpectedTokenException Unexpected "?"
Equality operators
Section titled “Equality operators”The is keyword checks whether the subject is of the compared type, and
is not checks the opposite. Both keywords are written in lower case only:
IS is an ordinary name, not a keyword. Whitespace and comments are allowed
between the words of is not.
Positive equality check.
T is int ? A : B
resultTypeLang\Type\Condition\ConditionalTypeNode
Negative equality check.
T is not int ? A : B
resultTypeLang\Type\Condition\ConditionalTypeNode
A comment between the words of is not.
T is /* comment */ not int ? A : B
resultTypeLang\Type\Condition\ConditionalTypeNode
The PHP-like == and != operators are not recognized: use the is and
is not keywords instead.
T == int ? A : B
errorUnexpectedTokenException Unexpected "="
The else branch is required.
T is int ? A
errorMalformedSyntaxException The condition requires an else branch, ":" expected
Yoda-style conditions
Section titled “Yoda-style conditions”Since the subject and the compared type can both be arbitrary type expressions, the condition may also be written in Yoda-style, with the concrete type on the left and the subject (a type or a variable) on the right.
Yoda-style equality check with a named type as the subject.
int is T ? A : B
resultTypeLang\Type\Condition\ConditionalTypeNode
Yoda-style equality check with a variable as the subject.
int is $value ? A : B
resultTypeLang\Type\Condition\ConditionalTypeNode
A variable may also stand on both sides.
$value is not $other ? A : B
resultTypeLang\Type\Condition\ConditionalTypeNode