Option Type

An option type, called Option in Rust and OCaml or Maybe in Haskell, is a native sum type with exactly two cases: a value is present, wrapped as Some(x) or Just x, or it is absent, represented as None or Nothing. It is used instead of a nullable type to represent the possible absence of a value.

The key difference with a plain nullable type is enforcement: the wrapped value cannot be used directly. It must first be unwrapped through pattern matching or a combinator such as map/and_then, so the compiler guarantees that the None/Nothing case is always considered before the contained value is accessed.

PHP has no option type. Absence of a value is represented with null, combined with a nullable type declaration such as ?string. The nullsafe operator ?-> and null coalescing operator ?? make chaining safer, but nothing prevents dereferencing a possibly-null value directly and triggering a runtime error; the language does not force the check the way an option type does.

<?php

    function findUser(int $id): ?User {
        return $id === 1 ? new User('Ada') : null;
    }

    // Nothing forces this check: omitting it just yields a runtime error.
    $user = findUser(2);
    echo $user?->name ?? 'not found';

?>

Documentation

Related : Nullsafe, Sum Type, Algebraic Data Type, Union Type, Pattern Matching