PHP code example of ghostwriter / option
1. Go to this page and download the library: Download ghostwriter/option 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 / option example snippets
use Ghostwriter\Option\Exception\NullPointerException;
use Ghostwriter\Option\None;
use Ghostwriter\Option\Some;
$greeting = Some::new('Hello World!');
echo $greeting->unwrap(); // 'Hello World!'
$name = None::new();
echo $name->unwrap(); // throw `NullPointerException`
echo $name->unwrapOr('Default Value'); // 'Default Value'
None::new(); // return `None`
Some::nullable(null); // return `None`
Some::new(null); // throw `NullPointerException`
--- Example
function divide(int $x, int $y): OptionInterface
{
if ($y === 0) {
return None::new();
}
return Some::new($x / $y);
}
divide(1, 0); // None
divide(1, 1); // Some(1)