Download the PHP package khalyomede/monad without Composer
On this page you can find all versions of the php package khalyomede/monad. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download khalyomede/monad
More information about khalyomede/monad
Files in khalyomede/monad
Package monad
Short Description Maybe, Option and Result monads.
License MIT
Homepage https://github.com/khalyomede/monad
Informations about the package monad
khalyomede/monad
Maybe, Option and Result monads for PHP.
Summary
- About
- Features
- Prerequisites
- Installation
- Examples
- API
About
I published on reddit an RFC proposal for what I called "enums constructor" (which happened to be an existing concept called "tagged unions"). Out of one comment, I shown how to chain calls to various ADT outcomes, in order to reduces pyramidal matches.
This got me into wanting to use more monads right now on my next projects. After searching on packagist.org, I did not found something that fits what I expected, so I decided to make my own version of how I think monads should look like in PHP.
Features
- Supports theses monads:
- Option
- Maybe
- Result
- PHPStan and static analyzers friendly
Prerequisites
- PHP 8+ installed
Installation
Examples
- 1. Getting a file content
- 2. Get users from an SQL table
- 3. Chain multiple outcomes
1. Getting a file content
In this example, we will use the Maybe monad as result, and use it in our main code.
2. Get users from an SQL table
In this example, we will use the Result monad to return the list of users or an error from PDO.
3. Chain multiple outcomes
In this example, we will chain multiple times "then", similar to what is possible in other languages through "map".
If you remove the file, the result becomes
If you remove the write permission on this file, the result becomes
API
For a list of all possible functions, see the tests\unit folder.