The Option type in PHP is designed to handle the presence or absence of a value explicitly as part of its state.
This allows developers to write more robust code by avoiding common errors like type errors and undefined indices.
Table of Contents:
- Creating an Option
- Querying the variant
- Extracting the contained value
- Transforming contained values
- Boolean operators
- Iterating over
Option - Comparison operators
- Shortcut functions
Creates an Option instance containing a non-null value. If null is passed, a LogicOptionException is thrown.
$opt = new Some(10);
echo $opt->unwrap(); // Outputs: 10
new Some(null); // Throws LogicOptionExceptionCreates an Option instance representing the absence of a value (i.e., None).
$opt = new None();
echo $opt->isNone(); // Outputs: trueConverts a value to an Option, making it None if the original value is null, otherwise Some.
$some = OptionFactory::from(5);
$none = OptionFactory::from(null);
echo $some->isSome(); // Outputs: true
echo $none->isNone(); // Outputs: trueCreates a copy of the option. Useful for preserving immutability when needed.
$x = new Some(2);
$y = $x->clone();
echo $y->unwrap(); // Outputs: 2Checks if the option is a Some value.
$opt = new Some(10);
echo $opt->isSome(); // Outputs: trueChecks if the option is a None value.
$opt = new None();
echo $opt->isNone(); // Outputs: trueReturns the contained value if it is Some; otherwise, throws a RuntimeOptionException with a custom message.
$opt = new Some(10);
echo $opt->expect('A number.'); // Outputs: 10
$opt = new None();
echo $opt->expect('A number.'); // Throws RuntimeOptionException with custom messageReturns the contained value if it is Some; otherwise, throws a RuntimeOptionException.
$opt = new Some(10);
echo $opt->unwrap(); // Outputs: 10
$opt = new None();
echo $opt->unwrap(); // Throws RuntimeOptionExceptionReturns the contained value if it is Some; otherwise, returns a provided default value.
$opt = new Some(10);
echo $opt->unwrapOr(5); // Outputs: 10
$opt = new None();
echo $opt->unwrapOr(5); // Outputs: 5Returns the contained value if it is Some; otherwise, computes a value using a provided callable.
$opt = new Some(10);
echo $opt->unwrapOrElse(fn () => 5); // Outputs: 10
$opt = new None();
echo $opt->unwrapOrElse(fn () => 5); // Outputs: 5Returns the contained value if it is Some; otherwise, throws the provided throwable error.
$opt = new Some(10);
echo $opt->unwrapOrThrow(new \Exception("No value!")); // Outputs: 10
$opt = new None();
echo $opt->unwrapOrThrow(new \Exception("No value!")); // Throws ExceptionConverts from Option<Option<T>> to Option<T>. Useful for unwrapping nested options.
$x = new Some(new Some(2));
echo $x->flatten()->unwrap(); // Outputs: 2
$x = new Some(new None());
echo $x->flatten()->isNone(); // Outputs: trueApplies a function to the contained value if it is Some, otherwise applies another function.
$x = new Some(2);
echo $x->match(
some: fn ($v) => $v * 2,
none: fn () => 0,
); // Outputs: 4
$x = new None();
echo $x->match(
some: fn ($v) => $v * 2,
none: fn () => 0,
); // Outputs: 0Transforms the contained value by applying a function if it is Some; returns None if it is None.
$opt = new Some(5);
$result = $opt->map(fn ($x) => $x * 2);
echo $result->unwrap(); // Outputs: 10
$opt = new None();
$result = $opt->map(fn ($x) => $x * 2);
echo $result->isNone(); // Outputs: trueApplies a function to the contained value if it is Some, otherwise returns a default value.
$opt = new Some(5);
echo $opt->mapOr(fn ($x) => $x * 2, 0); // Outputs: 10
$opt = new None();
echo $opt->mapOr(fn ($x) => $x * 2, 0); // Outputs: 0Applies a function to the contained value if it is Some, otherwise computes the default using another callable.
$opt = new Some(5);
echo $opt->mapOrElse(fn ($x) => $x * 2, fn () => 0); // Outputs: 10
$opt = new None();
echo $opt->mapOrElse(fn ($x) => $x * 2, fn () => 0); // Outputs: 0Applies a function to the contained value if it is Some, otherwise returns None. This is also known as flatmap
in other languages.
$x = new Some(2);
$result = $x->andThen(fn ($value) => new Some($value * 2));
echo $result->unwrap(); // Outputs: 4
$x = new None();
$result = $x->andThen(fn ($value) => new Some($value * 2));
echo $result->isNone(); // Outputs: trueReturns Some if the option is Some and the predicate returns true; otherwise, returns None.
$isEven = fn ($x) => $x % 2 === 0;
$x = new Some(4);
echo $x->filter($isEven)->isSome(); // Outputs: true
$x = new Some(5);
echo $x->filter($isEven)->isNone(); // Outputs: trueReturns the option itself if it is Some, otherwise returns the provided option.
$x = new Some(2);
$y = new Some(3);
echo $x->or($y)->unwrap(); // Outputs: 2
$x = new None();
echo $x->or($y)->unwrap(); // Outputs: 3Returns the option itself if it is Some, otherwise calls a callable to provide an option.
$x = new Some(2);
$y = new Some(3);
echo $x->orElse(fn () => $y)->unwrap(); // Outputs: 2
$x = new None();
echo $x->orElse(fn () => $y)->unwrap(); // Outputs: 3Returns Some if exactly one of the options is Some, otherwise returns None.
$x = new Some(2);
$y = new Some(3);
echo $x->xor($y)->isNone(); // Outputs: true
$x = new None();
echo $x->xor($y)->unwrap(); // Outputs: 3Returns the provided option if the original option is Some, otherwise returns None.
$x = new Some(2);
$y = new Some(3);
echo $x->and($y)->unwrap(); // Outputs: 3
$x = new None();
echo $x->and($y)->isNone(); // Outputs: trueProvides an iterator over the contained value if it is Some, otherwise an empty iterator.
$x = new Some(2);
foreach ($x->iterate() as $v) {
echo $v; // Outputs: 2
}
$x = new None();
foreach ($x->iterate() as $v) {
// 0 iterations
}Compares two options for equality. Returns true if both are None or if both are Some with equal values.
$x = new Some(2);
$y = new Some(2);
echo $x->equals($y); // Outputs: true
$x = new None();
$y = new None();
echo $x->equals($y); // Outputs: trueCreates an Option instance containing a non-null value. If null is passed, a LogicOptionException is thrown.
$opt = some(10); // Same as new Some(10)
echo $opt->unwrap(); // Outputs: 10
some(null); // Throws LogicOptionExceptionCreates an Option instance representing the absence of a value (i.e., None).
$opt = none(); // Same as new None()
echo $opt->isNone(); // Outputs: trueSee the examples for more details on how to apply effectively the Option type in your PHP code.