PHP code example of ghostwriter / result
1. Go to this page and download the library: Download ghostwriter/result 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/ */
ghostwriter / result example snippets
use Ghostwriter\Result\Failure;
use Ghostwriter\Result\Success;
// --- Success ---
$success = Success::new('Hello world!');
$success->get(); // 'Hello world!'
// --- Failure ---
$failure = Failure::new(new ExampleException());
$failure->get(); // throws: ResultException
$failure->getOr('Fallback'); // 'Fallback'
$failure->getError(); // returns: instance of ExampleException
// --- Example ---
function divide(int $x, int $y): ResultInterface
{
if ($y === 0) {
return Result::failure(new DivisionByZeroError);
}
return Result::success($x / $y);
}
divide(1, 0); // Error(DivisionByZeroError)
divide(1, 1); // Success(1)