PHP code example of yitznewton / maybe-php

1. Go to this page and download the library: Download yitznewton/maybe-php 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/ */

    

yitznewton / maybe-php example snippets


$blogpost = $repository->get($blogpostId);
echo $blogpost->teaser();  // oh noe! what if $blogpost is null?! :boom:

$blogpost = new \Yitznewton\Maybe\Maybe($repository->get($blogpostId));
echo $blogpost->select(function ($bp) { $bp->teaser(); })->valueOr('No blogpost found');

$blogpost = new \Yitznewton\Maybe\Maybe($repository->get($blogpostId));
$callback = function () {
    return someExpensiveOperation();
};
echo $blogpost->select(function ($bp) { $bp->teaser(); })->valueOrCallback($callback);

// $process->execute() normally returns a result object, but sometimes returns false
$result = new LooseMaybe($process->execute());

echo $result->select(function ($resultObject) { $resultObject->getStatus(); })->valueOr('failed');
// echoes 'failed' when the result was false

$dictionary = new \Yitznewton\Maybe\Dictionary([
    'foo' => 'bar',
]);

$dictionary->foo->valueOr('quux');        // 'bar'
$dictionary->noSuchKey->valueOr('quux');  // 'quux'

// with LooseMaybe

$dictionary = new \Yitznewton\Maybe\Dictionary([
    'foo' => false,
], \Yitznewton\Maybe\LooseMaybe::class);

$dictionary->foo->valueOr('quux'); // 'quux', because loose falsy