PHP code example of jefvda / php-result-monad

1. Go to this page and download the library: Download jefvda/php-result-monad library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jefvda / php-result-monad example snippets


/**
 * @param mixed $value The value associated with the result.
 * @return Result The successful Result which 

/**
 * @param Exception $exception The exception that explains why the result was not successful.
 * @return Result The not successful Result that 

/**
 * @param callable $valueCallback The callback for successful results.
 * @param callable $exceptionCallback The callback for unsuccessful results.
 * @return mixed The return value of the callback that has been called.
 */
public function match(callable $valueCallback, callable $exceptionCallback): mixed

// Create a successful result with a value
$successResult = Result::createFromValue('Hello, Result!');

// Create an unsuccessful result with an exception
$exception = new \Exception('Something went wrong.');
$failureResult = Result::createFromException($exception);

// Match on the results
$successMessage = $successResult->match(
    fn ($value) => 'Success: ' . $value,
    fn ($exception) => 'Failure: ' . $exception,
);

$failureMessage = $failureResult->match(
    fn ($value) => 'Success: ' . $value,
    fn ($exception) => 'Failure: ' . $exception,
);

echo $successMessage; // Will display 'Success: Hello, Result!' -> as the `Result` was successful
echo $failureMessage; // Will display 'Failure: Something went wrong.' -> as the `Result` was not successful