PHP code example of sayuprc / result-type
1. Go to this page and download the library: Download sayuprc/result-type 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/ */
sayuprc / result-type example snippets
use ResultType\Err;
use ResultType\Ok;
use ResultType\Result;
class Success
{
// Some data
}
class Error
{
// Some error
}
class Handler
{
/**
* @return Result<Success, Error>
*/
function handle(): Result
{
if (/* some error */) {
return new Err(new Error());
}
return new Ok(new Success());
}
}
$handler = new Handler();
$result = $handler->handle();
if ($result->isOk()) {
$result->unwrap(); // Access to Success
} else {
$result->unwrapErr(); // Access to Error
}