PHP code example of phonad / core
1. Go to this page and download the library: Download phonad/core 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/ */
phonad / core example snippets
use Phonad\Core\Data\Maybe;
$val = Maybe::of(10)
->map(fn($n) => $n * 2)
->orElse(0); // 20
use Phonad\Core\Data\Either;
$res = Either::Right(10)
->flatMap(fn($n) => $n > 5 ? Either::Right($n) : Either::Left("Too small"))
->either(fn($err) => "Error: $err", fn($val) => "Value: $val");
use Phonad\Core\Data\ImmutableList;
$list = ImmutableList::from([1, 2, 3])
->cons(0)
->filter(fn($n) => $n > 0); // [1, 2, 3]
use Phonad\Core\Effect\Reader;
$logic = Reader::ask()->map(fn($env) => "Env is: " . $env['mode']);
echo $logic->run(['mode' => 'production']);
use Phonad\Core\Effect\Writer;
[$val, $logs] = Writer::of(10)
->flatMap(fn($n) => Writer::of($n + 1, ["Incremented"]))
->run();
use Phonad\Core\Effect\IO;
// Wrap the effect into the `IO` monad
$sayHello = fn(string $name) => IO::make(function () use ($name) {
echo "Hello, $name!";
});
// Chain the logic (nothing actually happens yet)
$program = IO::of('world')
->map('strtoupper')
->flatMap(fn($name) => $sayHello($name));
// FIRE!
$program->run(); // Outputs: Hello, WORLD!