You can declare a type as nullable by prefixing a ? sign to the type. For example, if a certain property can be a string or a null, you can declare it as ?string. With PHP 8.0 Union Types, string|null will be functionally equivalent to ?string.
Also, ? is incompatible with union types. ?string|int is not accepted.
I'd like modify Type::nullable() to detect union type and add |null instead of ?.
Alternatively, Type::union() could accept a 2nd parameter: $nullable that adds the |null
From PHPWatch: PHP 8.0: Union Types
Also,
?
is incompatible with union types.?string|int
is not accepted.I'd like modify Type::nullable() to detect union type and add
|null
instead of?
.Alternatively,
Type::union()
could accept a 2nd parameter:$nullable
that adds the|null
I'll create a PR if that sounds good for you.
Note: intersection types are not nullable, RFC was declined.