hyperf/collection
A lightweight, fluent Collection implementation for Hyperf/PHP. Provides convenient methods for mapping, filtering, reducing, grouping, plucking, and more, with an API similar to Laravel Collections for handling arrays and iterables cleanly and safely.
Pros:
Illuminate\Support\Collection, offering consistent API for developers familiar with Laravel’s ecosystem.map, filter, pluck) align with modern PHP best practices.Collection<int, Model>).Cons:
Collection class (e.g., lacks Eloquent model integration, query builder methods).Illuminate/Collections).Collection to prevent method ambiguity.where, sort, first) are stable, but edge cases (e.g., coroutine handling) may evolve.Collection (unit tests for chained methods may need updates).Collection?
use Hyperf\Collection\Collection as HyperCollection)?Collection?Collection changes.Collection and native PHP arrays for critical paths.@return Collection<int, T>).| Component | Fit Level | Notes |
|---|---|---|
| Laravel (9+) | High | API parity with Illuminate/Collections; minimal refactoring needed. |
| Hyperf | Perfect | Native integration; coroutine-aware methods. |
| Standalone PHP | High | Drop-in replacement for ArrayObject/SplObjectStorage. |
| Legacy Laravel (<8) | Low | API differences may require wrapper classes. |
| Symfony/DDD | Medium | Useful for repositories/DTOs but lacks Symfony-specific optimizations. |
Collection usages in the codebase (find via grep -r "new Collection" or IDE search).$collection->where()->pluck()->sort()) for compatibility testing.Collection with Hyperf\Collection.bind() or Hyperf’s define).Illuminate\Collection or Hyperf\Collection based on config.| Feature | Laravel Collection |
Hyperf Collection |
Workaround |
|---|---|---|---|
| Eloquent model methods | ✅ (e.g., first()) |
❌ | Use first() + manual model resolution. |
| Query builder integration | ✅ (e.g., cursor()) |
❌ | Avoid or mock. |
| Coroutine support | ❌ | ✅ (Hyperf-only) | Only use in Hyperf contexts. |
| Lazy collections | ✅ | ✅ | Identical API. |
| JSON serialization | ✅ (toJson()) |
✅ | Identical. |
Hyperf\Collection in new modules (e.g., gRPC services, CLI tools).Collection in high-traffic endpoints (e.g., /api/search).Collection in business logic layers (avoid UI layers first).Collection facade in favor of Hyperf\Collection.Collection (no Eloquent/Query Builder bloat).Collection (fewer Stack Overflow answers, tutorials).Hyperf\Collection instead of Illuminate\Support\Collection (minor confusion).map/filter operations.Collection (no Eloquent metadata).collect($items)->each(fn($item) => go(fn() => process($item)))).| Scenario | Risk | Mitigation |
|---|---|---|
| API breaking change in v4.0 | Medium | Pin to ^3.1 until stable. |
| Coroutine deadlocks | Low | Avoid mixing sync/async collection ops. |
| Memory leaks in lazy collections | Low | Use tap() to debug resource usage. |
| IDE autocompletion failures | Low | Add @mixin PHPDoc for Laravel methods. |
How can I help you explore Laravel packages today?