phunkie/effect
Composer 安装命令:
composer require phunkie/effect
包简介
A functional effects library for PHP inspired by Scala's cats-effect
README 文档
README
A functional effects library for PHP inspired by Scala's cats-effect. Phunkie Effect provides a way to manage side effects in a purely functional way, making your code more predictable, testable, and maintainable.
Installation
composer require phunkie/effect
Requirements
- PHP 8.2 or higher
Basic Usage
IO Monad
The IO monad is the primary way to handle effects in Phunkie Effects. It allows you to wrap side effects in a pure functional context. Following Phunkie's design philosophy, we use traits to extend types and functions to construct and manipulate them.
use Phunkie\Effect\Functions\io\io; use Phunkie\Effect\IO\IO; // Create an IO from a pure value using the io function $pure = io(42); // Create an IO from a side effect $effect = io(function() { return file_get_contents('data.txt'); }); // Map over an IO using the FunctorOps trait $mapped = $effect->map(function($content) { return strtoupper($content); }); // Chain IOs using the MonadOps trait $chained = $effect->flatMap(function($content) { return io(function() use ($content) { return file_put_contents('output.txt', $content); }); });
Async Execution with start()
The start() method allows you to fork computations into background fibers, enabling fire-and-forget patterns:
use function Phunkie\Effect\Functions\io\io; // Define an async operation $sendEmail = io(function() use ($user) { mail($user->email, 'Welcome!', '...'); return 'sent'; }); // Fork to background and continue immediately $program = $sendEmail ->start() // Returns IO<AsyncHandle<string>> ->map(function($handle) { // Continue with other work... return 'Email queued'; }); // Or await the result later $program = $sendEmail ->start() ->flatMap(function($handle) { // Do other work here... $otherWork = io(fn() => 'other work done'); return $otherWork->map(function($result) use ($handle) { // Now wait for email to finish $emailResult = $handle->await(); return [$result, $emailResult]; }); }); // Custom execution context use Phunkie\Effect\Concurrent\ParallelExecutionContext; $heavyComputation = io(fn() => processLargeDataset()); $handle = $heavyComputation ->start(new ParallelExecutionContext()) // Use parallel threads ->unsafeRun();
Features
- Pure functional effect handling
- Type-safe IO operations
- Composable effect chains
- Error handling through Either
- Resource management
- Async execution with
start()- Fork computations to background fibers - Custom execution contexts - Control how effects are executed
Why Phunkie Effects?
- Type Safety: Catch errors at compile time
- Referential Transparency: Same input always produces the same output
- Testability: Easier to test pure functions
- Composability: Build complex programs from simple, pure functions
- Resource Management: Safe handling of resources like files and network connections
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
- Inspired by cats-effect
- Built on top of Phunkie
phunkie/effect 适用场景与选型建议
phunkie/effect 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 200 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 06 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 phunkie/effect 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 phunkie/effect 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 200
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 21
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-06-28