PHP Monad API Reference

PhpMonad

Table of Contents

Interfaces

Monad
Monad interface.
Option
Option monad as a `Maybe monad`.
Result
Result monad as a `Either monad`.

Classes

Some
Err
Ok

Enums

None

Functions

some()  : Some<string|int, T>
Return a `Option\Some` option containing `$value`.
none()  : None
Return a `Option\None` option containing no values.
fromValue()  : NoneValue>)
Transform a value into an `Option`.
of()  : NoneValue>)
Execute a callable and transform the result into an `Option`.
tryOf()  : NoneValue>)
Execute a callable and transform the result into an `Option` as `Option\of()` does but also return `Option\None` if it an exception matching $exceptionClass was thrown.
flatten()  : Option<string|int, U>
Converts from `Option<Option<T>>` to `Option<T>`.
ok_or()  : Result<T, E>
Convert an Option to a Result.
traverse()  : Result<U|null, E>
Apply a function that returns Result if Some, or ok(null) if None.
transpose()  : Result<Option<string|int, U>, E>
Transposes an `Option` of a `Result` into a `Result` of an `Option`.
map()  : callable(Option<string|int, T>): Option<string|int, U>
Pipeline function: Maps the Some value using the callback.
andThen()  : callable(Option<string|int, T>): Option<string|int, U>
Pipeline function: Chains an Option-returning operation on Some value.
orElse()  : callable(Option<string|int, mixed>): Option<string|int, mixed|U>
Pipeline function: Handles None by calling an Option-returning function.
filter()  : callable(Option<string|int, mixed>): Option<string|int, mixed>
Pipeline function: Filters Some value by predicate, returning None if false.
inspect()  : callable(Option<string|int, mixed>): Option<string|int, mixed>
Pipeline function: Performs a side-effect on Some value, passing through the Option.
unwrapOr()  : callable(Option<string|int, mixed>): mixed
Pipeline function: Unwraps the Some value or returns the default.
unwrapOrElse()  : callable(Option<string|int, mixed>): mixed
Pipeline function: Unwraps the Some value or computes a default.
expect()  : callable(Option<string|int, mixed>): mixed
Pipeline function: Unwraps the Some value or throws RuntimeException with the message.
okOr()  : callable(Option<string|int, mixed>): Result<mixed, E>
Pipeline function: Converts Option to Result with a fixed error value.
okOrElse()  : callable(Option<string|int, mixed>): Result<mixed, E>
Pipeline function: Converts Option to Result with a lazy error value.
ok()  : Ok<string|int, U>
Return a `Result\Ok` Result containing `$value`.
err()  : Err<string|int, F>
Return a `Result\Err` result.
fromThrowable()  : Result<T, E>
Creates a Result from a Closure that may throw an exception.
flatten()  : Result<T, E>
Converts from `Result<Result<T, E>, E>` to `Result<T, E>`.
transpose()  : Option<string|int, Result<U, F>>
Transposes a `Result` of an `Option` into an `Option` of a `Result`.
map_all()  : Result<mixed, E>
Applies a callback to the values of multiple `Result`s if all are `Ok`.
flat_map_all()  : Result<mixed, E>
Applies a `Result`-returning callback to the values of multiple `Result`s if all are `Ok`.
combine()  : Result<bool, array<int, E>>
map()  : callable(Result<T, mixed>): Result<U, mixed>
Pipeline function: Maps the Ok value using the callback.
mapErr()  : callable(Result<mixed, E>): Result<mixed, F>
Pipeline function: Maps the Err value using the callback.
andThen()  : callable(Result<T, mixed>): Result<U, mixed>
Pipeline function: Chains a Result-returning operation on Ok value.
orElse()  : callable(Result<mixed, E>): Result<mixed, F>
Pipeline function: Handles Err by calling a Result-returning function.
inspect()  : callable(Result<T, mixed>): Result<T, mixed>
Pipeline function: Performs a side-effect on Ok value, passing through the Result.
inspectErr()  : callable(Result<mixed, E>): Result<mixed, E>
Pipeline function: Performs a side-effect on Err value, passing through the Result.
unwrapOr()  : callable(Result<mixed, mixed>): mixed
Pipeline function: Unwraps the Ok value or returns the default.
unwrapOrElse()  : callable(Result<mixed, E>): mixed
Pipeline function: Unwraps the Ok value or computes a default from the Err.
expect()  : callable(Result<mixed, mixed>): mixed
Pipeline function: Unwraps the Ok value or throws RuntimeException with the message.

Functions

some()

Return a `Option\Some` option containing `$value`.

some(T $value) : Some<string|int, T>
Parameters
$value : T
Tags
template
Return values
Some<string|int, T>

none()

Return a `Option\None` option containing no values.

none() : None
Return values
None

fromValue()

Transform a value into an `Option`.

fromValue(U $value[, NoneValue|null $noneValue = null ]) : NoneValue>)

It will be a Some option containing $value if $value is different from $noneValue (default null)

Parameters
$value : U
$noneValue : NoneValue|null = null
Tags
template
template
Return values
NoneValue>)

of()

Execute a callable and transform the result into an `Option`.

of(callable(): U $callback[, NoneValue|null $noneValue = null ]) : NoneValue>)

It will be a Some option containing the result if it is different from $noneValue (default null).

Parameters
$callback : callable(): U
$noneValue : NoneValue|null = null
Tags
template
template
Return values
NoneValue>)

tryOf()

Execute a callable and transform the result into an `Option` as `Option\of()` does but also return `Option\None` if it an exception matching $exceptionClass was thrown.

tryOf(callable(): U $callback[, NoneValue|null $noneValue = null ][, E> $exceptionClass = Exception::class ]) : NoneValue>)
Parameters
$callback : callable(): U
$noneValue : NoneValue|null = null
$exceptionClass : E> = Exception::class
Tags
template
template
template
throws
Throwable
Return values
NoneValue>)

flatten()

Converts from `Option<Option<T>>` to `Option<T>`.

flatten(Option<string|int, Option<string|int, U>> $option) : Option<string|int, U>
Parameters
$option : Option<string|int, Option<string|int, U>>
Tags
template
Return values
Option<string|int, U>

ok_or()

Convert an Option to a Result.

ok_or(Option<string|int, T$option, E $error) : Result<T, E>

Some($value) becomes ok($value). None becomes err($error).

Parameters
$option : Option<string|int, T>
$error : E
Tags
template
template
Return values
Result<T, E>

traverse()

Apply a function that returns Result if Some, or ok(null) if None.

traverse(Option<string|int, T$option, callable(T): Result<U, E$fn) : Result<U|null, E>
Parameters
$option : Option<string|int, T>
$fn : callable(T): Result<U, E>
Tags
template
template
template
Return values
Result<U|null, E>

transpose()

Transposes an `Option` of a `Result` into a `Result` of an `Option`.

transpose(Option<string|int, Result<U, E>> $option) : Result<Option<string|int, U>, E>

None will be mapped to Ok(None). Some(Ok(_)) and Some(Err(_)) will be mapped to Ok(Some(_)) and Err(_).

Parameters
$option : Option<string|int, Result<U, E>>
Tags
template
template
Return values
Result<Option<string|int, U>, E>

map()

Pipeline function: Maps the Some value using the callback.

map(callable(T): U $callback) : callable(Option<string|int, T>): Option<string|int, U>

Usage with PHP 8.5 pipeline operator: $option |> Option\map(fn($x) => $x * 2)

Parameters
$callback : callable(T): U
Tags
template
template
Return values
callable(Option<string|int, T>): Option<string|int, U>

andThen()

Pipeline function: Chains an Option-returning operation on Some value.

andThen(callable(T): Option<string|int, U$callback) : callable(Option<string|int, T>): Option<string|int, U>

Usage with PHP 8.5 pipeline operator: $option |> Option\andThen(fn($x) => findUser($x))

Parameters
$callback : callable(T): Option<string|int, U>
Tags
template
template
Return values
callable(Option<string|int, T>): Option<string|int, U>

orElse()

Pipeline function: Handles None by calling an Option-returning function.

orElse(callable(): Option<string|int, U$callback) : callable(Option<string|int, mixed>): Option<string|int, mixed|U>

Usage with PHP 8.5 pipeline operator: $option |> Option\orElse(fn() => getDefault())

Parameters
$callback : callable(): Option<string|int, U>
Tags
template
Return values
callable(Option<string|int, mixed>): Option<string|int, mixed|U>

filter()

Pipeline function: Filters Some value by predicate, returning None if false.

filter(callable(mixed): bool $predicate) : callable(Option<string|int, mixed>): Option<string|int, mixed>

Usage with PHP 8.5 pipeline operator: $option |> Option\filter(fn($x) => $x > 0)

Parameters
$predicate : callable(mixed): bool
Return values
callable(Option<string|int, mixed>): Option<string|int, mixed>

inspect()

Pipeline function: Performs a side-effect on Some value, passing through the Option.

inspect(callable(mixed): mixed $callback) : callable(Option<string|int, mixed>): Option<string|int, mixed>

Usage with PHP 8.5 pipeline operator: $option |> Option\inspect(fn($x) => logger()->info("Got: {$x}"))

Parameters
$callback : callable(mixed): mixed
Return values
callable(Option<string|int, mixed>): Option<string|int, mixed>

unwrapOr()

Pipeline function: Unwraps the Some value or returns the default.

unwrapOr(U $default) : callable(Option<string|int, mixed>): mixed

Usage with PHP 8.5 pipeline operator: $option |> Option\unwrapOr(0)

Parameters
$default : U
Tags
template
Return values
callable(Option<string|int, mixed>): mixed

unwrapOrElse()

Pipeline function: Unwraps the Some value or computes a default.

unwrapOrElse(callable(): mixed $callback) : callable(Option<string|int, mixed>): mixed

Usage with PHP 8.5 pipeline operator: $option |> Option\unwrapOrElse(fn() => computeDefault())

Parameters
$callback : callable(): mixed
Return values
callable(Option<string|int, mixed>): mixed

expect()

Pipeline function: Unwraps the Some value or throws RuntimeException with the message.

expect(string $message) : callable(Option<string|int, mixed>): mixed

Usage with PHP 8.5 pipeline operator: $option |> Option\expect('Value must be present')

Parameters
$message : string
Return values
callable(Option<string|int, mixed>): mixed

okOr()

Pipeline function: Converts Option to Result with a fixed error value.

okOr(E $err) : callable(Option<string|int, mixed>): Result<mixed, E>

Usage with PHP 8.5 pipeline operator: $option |> Option\okOr('not found')

Parameters
$err : E
Tags
template
Return values
callable(Option<string|int, mixed>): Result<mixed, E>

okOrElse()

Pipeline function: Converts Option to Result with a lazy error value.

okOrElse(callable(): E $err) : callable(Option<string|int, mixed>): Result<mixed, E>

Usage with PHP 8.5 pipeline operator: $option |> Option\okOrElse(fn() => new NotFoundException())

Parameters
$err : callable(): E
Tags
template
Return values
callable(Option<string|int, mixed>): Result<mixed, E>

ok()

Return a `Result\Ok` Result containing `$value`.

ok([U $value = true ]) : Ok<string|int, U>
Parameters
$value : U = true
Tags
template
Return values
Ok<string|int, U>

err()

Return a `Result\Err` result.

err(F $value) : Err<string|int, F>
Parameters
$value : F
Tags
template
Return values
Err<string|int, F>

fromThrowable()

Creates a Result from a Closure that may throw an exception.

fromThrowable(callable(): T $closure, callable(Throwable): E $errorHandler) : Result<T, E>
Parameters
$closure : callable(): T
$errorHandler : callable(Throwable): E
Tags
template
template
Return values
Result<T, E>

transpose()

Transposes a `Result` of an `Option` into an `Option` of a `Result`.

transpose(Result<Option<string|int, U>, F$result) : Option<string|int, Result<U, F>>

Ok(None) will be mapped to None. Ok(Some(_)) and Err(_) will be mapped to Some(Ok(_)) and Some(Err(_)).

Parameters
$result : Result<Option<string|int, U>, F>
Tags
template
template
Return values
Option<string|int, Result<U, F>>

map_all()

Applies a callback to the values of multiple `Result`s if all are `Ok`.

map_all(Closure $fn, Result<mixed, E...$results) : Result<mixed, E>

Returns the first Err encountered, or Ok wrapping the callback's return value.

Parameters
$fn : Closure
$results : Result<mixed, E>
Tags
template
Return values
Result<mixed, E>

flat_map_all()

Applies a `Result`-returning callback to the values of multiple `Result`s if all are `Ok`.

flat_map_all(Closure $fn, Result<mixed, E...$results) : Result<mixed, E>

Returns the first Err encountered, or the Result returned by the callback.

Parameters
$fn : Closure
$results : Result<mixed, E>
Tags
template
Return values
Result<mixed, E>

combine()

combine(Result<T, E...$results) : Result<bool, array<int, E>>
Parameters
$results : Result<T, E>
Tags
template
template
Return values
Result<bool, array<int, E>>

map()

Pipeline function: Maps the Ok value using the callback.

map(callable(T): U $callback) : callable(Result<T, mixed>): Result<U, mixed>

Usage with PHP 8.5 pipeline operator: $result |> Result\map(fn($x) => $x * 2)

Parameters
$callback : callable(T): U
Tags
template
template
Return values
callable(Result<T, mixed>): Result<U, mixed>

mapErr()

Pipeline function: Maps the Err value using the callback.

mapErr(callable(E): F $callback) : callable(Result<mixed, E>): Result<mixed, F>

Usage with PHP 8.5 pipeline operator: $result |> Result\mapErr(fn($e) => "Error: {$e}")

Parameters
$callback : callable(E): F
Tags
template
template
Return values
callable(Result<mixed, E>): Result<mixed, F>

andThen()

Pipeline function: Chains a Result-returning operation on Ok value.

andThen(callable(T): Result<U, F$callback) : callable(Result<T, mixed>): Result<U, mixed>

Usage with PHP 8.5 pipeline operator: $result |> Result\andThen(fn($x) => validate($x))

Parameters
$callback : callable(T): Result<U, F>
Tags
template
template
template
Return values
callable(Result<T, mixed>): Result<U, mixed>

orElse()

Pipeline function: Handles Err by calling a Result-returning function.

orElse(callable(E): Result<mixed, F$callback) : callable(Result<mixed, E>): Result<mixed, F>

Usage with PHP 8.5 pipeline operator: $result |> Result\orElse(fn($e) => recover($e))

Parameters
$callback : callable(E): Result<mixed, F>
Tags
template
template
Return values
callable(Result<mixed, E>): Result<mixed, F>

inspect()

Pipeline function: Performs a side-effect on Ok value, passing through the Result.

inspect(callable(T): mixed $callback) : callable(Result<T, mixed>): Result<T, mixed>

Usage with PHP 8.5 pipeline operator: $result |> Result\inspect(fn($x) => logger()->info("Got: {$x}"))

Parameters
$callback : callable(T): mixed
Tags
template
Return values
callable(Result<T, mixed>): Result<T, mixed>

inspectErr()

Pipeline function: Performs a side-effect on Err value, passing through the Result.

inspectErr(callable(E): mixed $callback) : callable(Result<mixed, E>): Result<mixed, E>

Usage with PHP 8.5 pipeline operator: $result |> Result\inspectErr(fn($e) => logger()->error($e))

Parameters
$callback : callable(E): mixed
Tags
template
Return values
callable(Result<mixed, E>): Result<mixed, E>

unwrapOr()

Pipeline function: Unwraps the Ok value or returns the default.

unwrapOr(U $default) : callable(Result<mixed, mixed>): mixed

Usage with PHP 8.5 pipeline operator: $result |> Result\unwrapOr(0)

Parameters
$default : U
Tags
template
Return values
callable(Result<mixed, mixed>): mixed

unwrapOrElse()

Pipeline function: Unwraps the Ok value or computes a default from the Err.

unwrapOrElse(callable(E): U $callback) : callable(Result<mixed, E>): mixed

Usage with PHP 8.5 pipeline operator: $result |> Result\unwrapOrElse(fn($e) => fallback($e))

Parameters
$callback : callable(E): U
Tags
template
template
Return values
callable(Result<mixed, E>): mixed

expect()

Pipeline function: Unwraps the Ok value or throws RuntimeException with the message.

expect(string $message) : callable(Result<mixed, mixed>): mixed

Usage with PHP 8.5 pipeline operator: $result |> Result\expect('Value must be present')

Parameters
$message : string
Return values
callable(Result<mixed, mixed>): mixed

        
On this page

Search results