PHP code example of baethon / union

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

    

baethon / union example snippets


class Maybe extends \Baethon\Union\AbstractUnion
{
}

class Maybe extends \Baethon\Union\AbstractUnion
{
	const SOME = 'Some:x';

	const NONE = 'None';
}

$some = Maybe::Some(1);

function addTen(Maybe $maybe) {
	return $maybe->matchWith([
		'Some' => function ($x) {
			return $x + 10;
		},
		'None' => function () {
			throw new \Exception('Sorry, can\'t add a number to nothing');
		}
	]);
}

addTen($some); // 11

$some->matchWith([
	'*' => function () {
		return 100;
	}
]); // 100

$some->matchWith([
	Maybe::SOME => function () {},
	Maybe::NONE => function () {}
]);