PHP Monad API Reference

Result

Table of Contents

Classes

Err
Ok

Functions

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

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