functional-php/fantasy-land
Fantasy Land–style algebraic structures for PHP to enable interoperable functional patterns. Includes interfaces for Setoid, Semigroup/Monoid, Functor, Applicative, Monad, Foldable/Traversable, plus ready-made law/property tests to verify implementations.
This package is not a traditional Laravel package—it’s a specification (aligned with the Fantasy Land standard) for functional programming constructs like Maybe, Either, IO, etc. To use it in a Laravel app, first install the implementation that conforms to this spec—e.g., webimpressions/fantasy-land or php-functional/php-fantasy-land. Then, require only the specific types you need (e.g., composer require webimpressions/fantasy-land). Start by replacing error-prone parts of your service layer with Maybe::of($value)->map(...)->getOrElse(...) to handle nullable values safely, avoiding NullPointerException risks.
Maybe, Either, or IO to model workflows with error handling (e.g., Either::fromTry(fn () => $this->fetchExternalApi())...fold(...)).Either to represent validation results—left for errors, right for success—without early exceptions.DB::table()->get() or Storage::get() in IO::of(fn () => ...) to defer execution and maintain referential transparency.request(), auth()->user()) in IO or Reader (if available in your implementation) to inject dependencies cleanly during testing.App\ValueObjects\Email that implements Map and Chain tocompose domain-safe transformations.map() without side effects; use tap() or chain() (if supported) only for side-effecting functions when absolutely necessary.peek() methods on your wrappers (Maybe, Either) to log intermediate values without breaking the chain.mapMaybe vs chainNullable). Lock your version and stick to one implementation across the project.class MyMaybe extends Maybe) to add domain-specific helpers like MyMaybe::fromUser(?User $user) to reduce boilerplate.Right/Left or Just/Nothing values without mocking.How can I help you explore Laravel packages today?