Skip to content

Nullable union type #139

@GromNaN

Description

@GromNaN

From PHPWatch: PHP 8.0: Union Types

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

I'll create a PR if that sounds good for you.

Note: intersection types are not nullable, RFC was declined.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions