PHP Monad API Reference

Result extends Monad

Result monad as a `Either monad`.

Inspired by Rust's Result enum.

Tags
see
https://doc.rust-lang.org/std/result/enum.Result.html
template
template
extends
Attributes
#[Sealed]
$permits: [\WizDevelop\PhpMonad\Result\Ok::class, \WizDevelop\PhpMonad\Result\Err::class]

Table of Contents

Methods

and()  : Result<U, E>
andThen()  : E>)
NOTE: PHPdoc's completion by type specification in Closure doesn't work, so I'm redefining it.
err()  : Option<string|int, E>
expect()  : T
inspect()  : $this
inspectErr()  : $this
isErr()  : bool
isErrAnd()  : bool
isOk()  : bool
isOkAnd()  : bool
map()  : Result<U, E>
mapErr()  : Result<T, F>
mapOr()  : U
mapOrElse()  : U
match()  : U|V
Applies one of two functions depending on whether the result is Ok or Err.
ok()  : Option<string|int, T>
or()  : Result<T, F>
orElse()  : Result<T, F>
orThrow()  : $this
Returns the contained Ok value or throws the provided exception.
unit()  : Monad<string|int, TValue>
`return` in Haskell. (`Unit` operation.)
unwrap()  : T
unwrapErr()  : E
unwrapOr()  : T|U
unwrapOrElse()  : T|U
unwrapOrThrow()  : T
Returns the contained Ok value or throws the provided exception.

Methods

match()

Applies one of two functions depending on whether the result is Ok or Err.

public match(callable(T): U $okFn, callable(E): V $errFn) : U|V

Comparable to neverthrow's match method.

Parameters
$okFn : callable(T): U

Function to apply if the Result is Ok

$errFn : callable(E): V

Function to apply if the Result is Err

Tags
template
template
Return values
U|V

The result of applying the appropriate function

orThrow()

Returns the contained Ok value or throws the provided exception.

public orThrow(F $exception) : $this
Parameters
$exception : F

The exception to throw if the result is Err

Tags
template
throws
F
Return values
$this

unit()

`return` in Haskell. (`Unit` operation.)

public static unit(TValue $value) : Monad<string|int, TValue>
Parameters
$value : TValue
Tags
template
Return values
Monad<string|int, TValue>

unwrapOrThrow()

Returns the contained Ok value or throws the provided exception.

public unwrapOrThrow(F $exception) : T
Parameters
$exception : F

The exception to throw if the result is Err

Tags
template
throws
F
Return values
T

        
On this page

Search results