1. Go to this page and download the library: Download serenitylabs/phatcats 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/ */
serenitylabs / phatcats example snippets
interface SemiGroup {
function append($left, $right);
}
class MaybeSemiGroup implements SemiGroup {
$innerSemiGroup;
public function __construct(SemiGroup $innerSemiGroup) {
$this->innerSemiGroup = $innerSemiGroup;
}
function append($left, $right) {
if ($left->isNothing()) {
$result = $right;
} else {
if ($right->isNothing()) {
$result = $left;
} else {
// both $left and $right are Just
$lVal = $left->get();
$rVal = $right->get();
$innerAppended = $this->innerSemiGroup->append($lVal, $rVal);
$result = Maybe::fromValue($innerAppended);
}
}
return $result;
}
}
trait ObjectSemiGroup {
abstract function append($appendee, SemiGroup $innerSemigroup);
}
$listFactory = new LinkedListFactory();
$emptyList = $listFactory->empty();
// Ugly, but gets the job done.
if ($myMaybe instanceof Just) {
$myVal = $myMaybe->get();
$response = response("<p>$myVal is: " . $myVal . ".</p>");
} else {
$response = response("There was no value!", 400);
}
// A little better.
if ($myMaybe->isNothing()) {
$response = response("There was no value!", 400);
} else {
$myVal = $myMaybe->get();
$response = response("<p>$myVal is: " . $myVal . ".</p>");
}
class MaybeToHttpResponse implements MaybeVisitor {
public function visitJust($just) {
$myVal = $just->get();
return response("<p>$myVal is: " . $myVal . ".</p>");
}
public function visitNothing($nothing) {
return response("There was no value!", 400);
}
}